Home »
Java programming language
Common String Operations in Java
By IncludeHelp Last updated : January 29, 2024
Strings in Java
String is a class in java, which provides some of the predefined methods that make string based problem solutions easier. We don’t need to write code for every operation, we have to just use its methods.
In this post, we are going to learn some of the most useful methods of the string.
Declaring and assigning value to string objet
There can be two different ways to declare and assign a string to an object in Java.
Declaration and then assignment
String msg; //declaration
msg = "Hello world"; // assignment
Declaration with initialization
String msg = "Hello world";
Checking equality of Java strings
The s1.equals(s2) method is used to compare two strings; it returns boolean values ‘true’/ ‘false’. If s1 and s2 are exactly same it returns ‘true’ otherwise it returns ‘false’.
Example
public class Main {
public static void main(String[] args) {
String s1 = "Hello world";
String s2 = "Hello world";
System.out.println(s1.equals(s2));
}
}
Both strings are exactly same, so the function will returns 'true'.
Checking equality of Java strings by ignoring case
The s1.equalsIgnoreCase(s2) method is used to compare two strings but it ignores the case, it returns boolean values ‘true’/ ‘false’. If s1 and s2 are same (by ignoring the case) it returns ‘true’ otherwise it returns ‘false’.
Example
public class Main {
public static void main(String[] args) {
String s1 = "Hello world";
String s2 = "HELLO world";
System.out.println(s1.equalsIgnoreCase(s2));
}
}
In this case, strings are same but characters are not in same case, still this function will return ‘true’.
Getting length of Java strings
The s1.length() method returns the length of the string s1 i.e. total number of characters of the string.
Example
public class Main {
public static void main(String[] args) {
String s1 = "Hello world!";
System.out.println(s1.length());
}
}
Total number of characters in the string "Hello world!" are 12. Therefore, this function will return 12.
Getting characters from Java string
The s1.charAt(N) method is used to get the character from Nth index of the string. Remember, string’s index starts from 0.
Example
public class Main {
public static void main(String[] args) {
String msg = "Hello world";
System.out.println(msg.charAt(0));
System.out.println(msg.charAt(6));
}
}
Output of this example with be "H" and "w" because "H" is at the 0th index and "w" is at the 6th index.
Read more: Java String | String.charAt(index) Method with Example
Getting starting index of a substring
The s1.indexOf(s2) method is used to get the starting index of any substring. Here, if substring s2 exists in the string s1, it will return starting position (index) of substring s2. If substring does not exist in the string, it returns -1.
Example 1: If substring exists in the string
public class Main {
public static void main(String[] args) {
String msg = "Hello world";
System.out.println(msg.indexOf("world"));
}
}
Output will be 6, because substring "world" initial index starts from 6th in the string "Hello world".
Example 2: If substring does not exist in the string
public class Main {
public static void main(String[] args) {
String msg = "Hello world";
System.out.println(msg.indexOf("Hi"));
}
}
Output will be -1, because substring "Hi" does not exist in the string "Hello world".
Checking substring in a Java string
The s1.substring(N,M) method is used to get the substring from the string. Here, function substring() will return the substring starting from Nth index to (M-1)th index.
Let suppose value of N is 6 and value of M is 11, then function will return 6th, 7th, 8th, 9thth and 10th character, it does not consist Mth character.
Example
public class Main {
public static void main(String[] args) {
String msg = "Hello world!";
System.out.println(msg.substring(6, 11));
}
}
Output will be "world".
Comparing Java strings
The s1.compareTo(s2) method is used to compare two strings, if both strings are same it returns 0, if string s1 is less than s2 it returns negative value, if string s2 is less than string s1 it returns positive value. Negative and positive value depends on the difference in the ASCII codes of first dissimilar characters.
Example
public class Main {
public static void main(String[] args) {
String s1, s2;
s1 = "Hello";
s2 = "Hello";
System.out.print(s1.compareTo(s2));
s1 = "Hello";
s2 = "HELLO";
System.out.println(s1.compareTo(s2));
s1 = "Hello";
s2 = "World";
System.out.println(s1.compareTo(s2));
}
}
Output
0
32
-15
Converting Java strings to uppercase
The s1.toUpperCase() method returns uppercase string.
Example
public class Main {
public static void main(String[] args) {
String msg = "Hello World!";
System.out.println(msg.toUpperCase());
}
}
Output will be "HELLO WORLD!".
Converting Java strings to lowercase
The s1.toLowerCase() method returns lowercase string.
Example
public class Main {
public static void main(String[] args) {
String msg = "Hello World!";
System.out.println(msg.toLowerCase());
}
}
Output will be "hello world!".
Trimming Java strings
The s1.trim() method returns the trimmed string after removing leading and trailing spaces.
Example
public class Main {
public static void main(String[] args) {
String msg = " Hello world! ";
System.out.println(msg.trim());
}
}
Output will be "Hello world!".