Strings methods in Java
charAt(int index): Returns the character at the String str = "Hello";
specified index. char ch = str.charAt(0); // Returns 'H'
length(): Returns the length of the string. String str = "Hello";
int length = str.length(); // Returns 5
substring(int beginIndex): Returns a substring String str = "Hello World";
from the specified index to the end of the String subStr = str.substring(6); // Returns
string. "World"
substring(int beginIndex, int endIndex): String str = "Hello World";
Returns a substring within the specified range. String subStr = str.substring(6, 11); // Returns
"World"
toLowerCase(): Converts all characters in the String str = "Hello";
string to lowercase. String lowerStr = str.toLowerCase(); // Returns
"hello"
toUpperCase(): Converts all characters in the String str = "Hello";
string to uppercase. String upperStr = str.toUpperCase(); // Returns
"HELLO"
trim(): Removes leading and trailing whitespace String str = " Hello ";
from the string. String trimmedStr = str.trim(); // Returns
"Hello"
equals(Object obj): Compares the string to the String str1 = "Hello";
specified object for equality. String str2 = "Hello";
boolean isEqual = str1.equals(str2); // Returns
true
equalsIgnoreCase(String anotherString): String str1 = "Hello";
Compares the string to another string, ignoring String str2 = "hello";
case differences. boolean isEqual = str1.equalsIgnoreCase(str2);
// Returns true
startsWith(String prefix): Checks if the string String str = "Hello";
starts with the specified prefix. boolean startsWithHello =
str.startsWith("Hello"); // Returns true
endsWith(String suffix): Checks if the string String str = "Hello";
ends with the specified suffix. boolean endsWithWorld =
str.endsWith("World"); // Returns false
contains(CharSequence s): Checks if the string String str = "Hello World";
contains the specified sequence of cha boolean containsWorld = str.contains("World");
// Returns true
indexOf(int ch): Returns the index of the first String str = "Hello";
occurrence of the specified character in the int index = str.indexOf('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 = str.lastIndexOf('l'); // Returns 3
string.
replace(char oldChar, char newChar): Replaces String str = "Hello World";
all occurrences of the specified old character String replacedStr = str.replace('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 = str.replaceAll("\\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 = str.split(","); // 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 = str1.concat(str2); //
Returns "Hello World"
isEmpty(): Checks if the string is empty String str = "";
(contains no characters). boolean isEmpty = str.isEmpty(); // Returns true
valueOf(Object obj): Returns the string int num = 123;
representation of the specified object. String str = String.valueOf(num); // Returns
"123"
startsWith(String prefix, int offset): Checks if String str = "Hello";
the string starts with the specified prefix, boolean startsWithEl = str.startsWith("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 = str.toCharArray();
// Display each character in the character
array
System.out.println("Characters in the
string:");
for (char ch : charArray) {
System.out.println(ch);
}
// Alternatively, you can access characters
by index
System.out.println("\nCharacters accessed
by index:");
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
System.out.println(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 java.util.Scanner;
prints them:
public class ReadAndPrintCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read a string from the user
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
// Print each character of the string
System.out.println("Characters in the
string:");
for (int i = 0; i < inputString.length(); i++) {
char ch = inputString.charAt(i);
System.out.println(ch);
}
scanner.close();
}
}
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 < stringDoubleArray.length;
i++) {
for (int j = 0; j <
stringDoubleArray[i].length; j++) {
System.out.print(stringDoubleArray[i][j] + " ");
}
System.out.println(); // Print new line
after each row
}
}
}
Also visit https://docs.oracle.com/javase/8/docs/api/java/lang/String.html for more methods