Strings methods in Java
charAt(int index): Returns the character at the String str = "Hello";
specified index. char ch = [Link](0); // Returns 'H'
length(): Returns the length of the string. String str = "Hello";
int length = [Link](); // Returns 5
substring(int beginIndex): Returns a substring String str = "Hello World";
from the specified index to the end of the String subStr = [Link](6); // Returns
string. "World"
substring(int beginIndex, int endIndex): String str = "Hello World";
Returns a substring within the specified range. String subStr = [Link](6, 11); // Returns
"World"
toLowerCase(): Converts all characters in the String str = "Hello";
string to lowercase. String lowerStr = [Link](); // Returns
"hello"
toUpperCase(): Converts all characters in the String str = "Hello";
string to uppercase. String upperStr = [Link](); // Returns
"HELLO"
trim(): Removes leading and trailing whitespace String str = " Hello ";
from the string. String trimmedStr = [Link](); // Returns
"Hello"
equals(Object obj): Compares the string to the String str1 = "Hello";
specified object for equality. String str2 = "Hello";
boolean isEqual = [Link](str2); // Returns
true
equalsIgnoreCase(String anotherString): String str1 = "Hello";
Compares the string to another string, ignoring String str2 = "hello";
case differences. boolean isEqual = [Link](str2);
// Returns true
startsWith(String prefix): Checks if the string String str = "Hello";
starts with the specified prefix. boolean startsWithHello =
[Link]("Hello"); // Returns true
endsWith(String suffix): Checks if the string String str = "Hello";
ends with the specified suffix. boolean endsWithWorld =
[Link]("World"); // Returns false
contains(CharSequence s): Checks if the string String str = "Hello World";
contains the specified sequence of cha boolean containsWorld = [Link]("World");
// Returns true
indexOf(int ch): Returns the index of the first String str = "Hello";
occurrence of the specified character in the int index = [Link]('e'); // Returns 1
string.
lastIndexOf(int ch): Returns the index of the String str = "Hello";
last occurrence of the specified character in the int lastIndex = [Link]('l'); // Returns 3
string.
replace(char oldChar, char newChar): Replaces String str = "Hello World";
all occurrences of the specified old character String replacedStr = [Link]('o', '0'); //
with the new character. Returns "Hell0 W0rld"
replaceAll(String regex, String replacement): String str = "Hello123";
Replaces all substrings that match the given String replacedStr = [Link]("\\d", "X"); //
regular expression with the specified Returns "HelloXXX"
replacement string.
split(String regex): Splits the string into an String str = "Hello,World";
array of substrings based on the specified String[] parts = [Link](","); // Returns ["Hello",
regular expression. "World"]
concat(String str): Concatenates the specified String str1 = "Hello";
string to the end of the original string. String str2 = " World";
String concatenatedStr = [Link](str2); //
Returns "Hello World"
isEmpty(): Checks if the string is empty String str = "";
(contains no characters). boolean isEmpty = [Link](); // Returns true
valueOf(Object obj): Returns the string int num = 123;
representation of the specified object. String str = [Link](num); // Returns
"123"
startsWith(String prefix, int offset): Checks if String str = "Hello";
the string starts with the specified prefix, boolean startsWithEl = [Link]("el", 1); //
beginning at the specified index. Returns true
string to an array of characters using the public class StringToCharExample {
toCharArray() method of the String class public static void main(String[] args) {
// String to character array using
toCharArray()
String str = "Hello";
char[] charArray = [Link]();
// Display each character in the character
array
[Link]("Characters in the
string:");
for (char ch : charArray) {
[Link](ch);
}
// Alternatively, you can access characters
by index
[Link]("\nCharacters accessed
by index:");
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
[Link](ch);
}
}
}
Output
Characters in the string:
H
e
l
l
o
Characters accessed by index:
H
e
l
l
o
example that reads characters from a string and import [Link];
prints them:
public class ReadAndPrintCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Read a string from the user
[Link]("Enter a string: ");
String inputString = [Link]();
// Print each character of the string
[Link]("Characters in the
string:");
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
[Link](ch);
}
[Link]();
}
}
create a double array (array of arrays) of strings public class StringDoubleArrayExample {
in Java public static void main(String[] args) {
// Define a two-dimensional array of
strings
String[][] stringDoubleArray = {
{"apple", "banana", "cherry"},
{"orange", "grape", "pear"},
{"kiwi", "melon", "pineapple"}
};
// Access and print elements of the double
array
for (int i = 0; i < [Link];
i++) {
for (int j = 0; j <
stringDoubleArray[i].length; j++) {
[Link](stringDoubleArray[i][j] + " ");
}
[Link](); // Print new line
after each row
}
}
}
Also visit [Link] for more methods