Java String Methods are a set of built-in functions that are used to manipulate strings in Java programming language. Strings are a sequence of characters that are used to represent text in Java. The Java String Methods provide a wide range of functionalities to work with strings such as concatenation, comparison, searching, replacing, and many more.
Here are some of the most commonly used Java String Methods:
The length() method is used to find the length of a string. It returns the number of characters in the string.
String str = "Hello World";
int len = str.length();
System.out.println("Length of the string is: " + len);
Output:
Length of the string is: 11
The charAt() method is used to find the character at a specified index in a string. The index starts from 0.
String str = "Hello World";
char ch = str.charAt(4);
System.out.println("Character at index 4 is: " + ch);
Output:
Character at index 4 is: o
The substring() method is used to extract a substring from a string. It takes two parameters, the starting index and the ending index (exclusive).
String str = "Hello World";
String substr = str.substring(6, 11);
System.out.println("Substring is: " + substr);
Output:
Substring is: World
The equals() method is used to compare two strings for equality. It returns true if the strings are equal, otherwise false.
String str1 = "Hello";
String str2 = "Hello";
boolean result = str1.equals(str2);
System.out.println("Strings are equal: " + result);
Output:
Strings are equal: true
The compareTo() method is used to compare two strings lexicographically. It returns a negative integer, zero, or a positive integer as the first string is less than, equal to, or greater than the second string.
String str1 = "Hello";
String str2 = "World";
int result = str1.compareTo(str2);
System.out.println("Result of comparison is: " + result);
Output:
Result of comparison is: -15
The indexOf() method is used to find the index of a specified character or substring in a string. It returns the index of the first occurrence of the character or substring, or -1 if it is not found.
String str = "Hello World";
int index = str.indexOf("o");
System.out.println("Index of 'o' is: " + index);
Output:
Index of 'o' is: 4
The replace() method is used to replace a character or substring in a string with another character or substring.
String str = "Hello World";
String newStr = str.replace("World", "Java");
System.out.println("New string is: " + newStr);
Output:
New string is: Hello Java
The toUpperCase() method is used to convert a string to uppercase.
String str = "Hello World";
String upperStr = str.toUpperCase();
System.out.println("Uppercase string is: " + upperStr);
Output:
Uppercase string is: HELLO WORLD
The toLowerCase() method is used to convert a string to lowercase.
String str = "Hello World";
String lowerStr = str.toLowerCase();
System.out.println("Lowercase string is: " + lowerStr);
Output:
Lowercase string is: hello world
These are just a few examples of the many Java String Methods available. They provide a powerful set of tools for working with strings in Java programming language.