Important String Library Functions in Java
1. length()
● Description: Returns the length of the string.
Example:
java
Copy code
String str = "Hello";
[Link]([Link]()); // Output: 5
●
2. charAt(int index)
● Description: Returns the character at the specified index.
Example:
java
Copy code
String str = "Hello";
[Link]([Link](1)); // Output: e
●
3. substring(int beginIndex, int endIndex)
● Description: Returns a substring from beginIndex (inclusive) to endIndex
(exclusive).
Example:
java
Copy code
String str = "Hello World";
[Link]([Link](0, 5)); // Output: Hello
●
4. contains(CharSequence sequence)
● Description: Checks if the string contains the specified sequence.
Example:
java
Copy code
String str = "Hello World";
[Link]([Link]("World")); // Output: true
●
5. toLowerCase() and toUpperCase()
● Description: Converts the string to lowercase or uppercase.
Example:
java
Copy code
String str = "Hello World";
[Link]([Link]()); // Output: hello world
[Link]([Link]()); // Output: HELLO WORLD
●
6. trim()
● Description: Removes leading and trailing whitespace from the string.
Example:
java
Copy code
String str = " Hello World ";
[Link]([Link]()); // Output: Hello World
●
7. equals() and equalsIgnoreCase()
● Description: Compares two strings for equality, case-sensitive (equals()) or
case-insensitive (equalsIgnoreCase()).
Example:
java
Copy code
String str1 = "Hello";
String str2 = "hello";
[Link]([Link](str2)); // Output: false
[Link]([Link](str2)); // Output: true
●
8. replace(char oldChar, char newChar)
● Description: Replaces occurrences of a character with another character.
Example:
java
Copy code
String str = "Hello World";
[Link]([Link]('o', 'a')); // Output: Hella Warld
●
9. split(String regex)
● Description: Splits the string around matches of the regex.
Example:
java
Copy code
String str = "Java is fun";
String[] words = [Link](" ");
for (String word : words) {
[Link](word);
}
// Output:
// Java
// is
// fun
●
10. indexOf() and lastIndexOf()
● Description: Finds the first or last occurrence of a character or substring.
Example:
java
Copy code
String str = "Hello World";
[Link]([Link]('o')); // Output: 4
[Link]([Link]('o')); // Output: 7
●
Frequently Asked String Interview Questions
1. Reverse a String
Problem: Write a program to reverse a string without using the reverse function. Solution:
java
Copy code
public class ReverseString {
public static void main(String[] args) {
String str = "Interview";
StringBuilder reversed = new StringBuilder();
for (int i = [Link]() - 1; i >= 0; i--) {
[Link]([Link](i));
}
[Link]("Reversed String: " + reversed);
}
}
2. Check if Two Strings are Anagrams
Problem: Write a program to check if two strings are anagrams.
Solution:
java
Copy code
import [Link];
public class AnagramCheck {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
char[] arr1 = [Link]();
char[] arr2 = [Link]();
[Link](arr1);
[Link](arr2);
if ([Link](arr1, arr2)) {
[Link]("The strings are anagrams.");
} else {
[Link]("The strings are not anagrams.");
}
}
}
3. Count the Frequency of Characters in a String
Problem: Write a program to count the frequency of each character in a string.
Solution:
java
Copy code
import [Link];
public class CharFrequency {
public static void main(String[] args) {
String str = "hello world";
HashMap<Character, Integer> freq = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
[Link](freq);
}
}
4. Check if a String is a Palindrome
Problem: Write a program to check if a string is a palindrome.
Solution:
java
Copy code
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
if ([Link](reversed)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
}
}
5. Find the Longest Palindromic Substring
Problem: Write a program to find the longest palindromic substring in a string.
Solution:
java
Copy code
public class LongestPalindrome {
public static void main(String[] args) {
String str = "babad";
String result = "";
for (int i = 0; i < [Link](); i++) {
for (int j = i; j < [Link](); j++) {
String sub = [Link](i, j + 1);
if (isPalindrome(sub) && [Link]() >
[Link]()) {
result = sub;
}
}
}
[Link]("Longest Palindromic Substring: " +
result);
}
public static boolean isPalindrome(String s) {
return [Link](new StringBuilder(s).reverse().toString());
}
}
6. Count Vowels and Consonants
Problem: Write a program to count vowels and consonants in a string.
Solution:
java
Copy code
public class VowelConsonantCount {
public static void main(String[] args) {
String str = "Java Programming";
int vowels = 0, consonants = 0;
for (char c : [Link]().toCharArray()) {
if (c >= 'a' && c <= 'z') {
if ("aeiou".indexOf(c) != -1) {
vowels++;
} else {
consonants++;
}
}
}
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);
}
}