String Methods

Advanced String methods

charAt() method 

This method is used to look at a String as a sequence of characters and return the character which is present at a particular position input by the user.

Example:

Try It
public class CharAt 
{
	public static void main(String[] args) 
	{
		String a = "Welcome To Java";
		int charAtPosition = 5;
		char charValue = a.charAt(charAtPosition);
		System.out.println("Char At " + charAtPosition + " is :: " + charValue);
	}
}

Click "Try It" button to execute above code. Output will be:

Char At 5 is :: m

Explanation:

Clearly, the character at position 5 is: m

trim() method

trim() method is used to remove any space characters at the start or the end of a String. This method also very widely used.

Example:

Try It
public class Trim 
{
	public static void main(String[] args) 
	{
		String a = "Welcome To Java      ";
		System.out.println("Before Trimming :: \""+a+"\"");
		String output = a.trim();
		System.out.println("Before Trimming :: \""+output+"\"");
	}
}

Click "Try It" button to execute above code. Output will be:

Before Trimming :: "Welcome To Java "
Before Trimming :: "Welcome To Java"

toUpperCase(), toLowerCase() method 

toUpperCase() method is used to convert a string into uppercase, whereas toLowerCase() is used to convert whole string in lower case. 

Example:

Try It
public class UpperLowerCase 
{
	public static void main(String[] args) 
	{
		String a = "Welcome To Java";
		String upper_String = a.toUpperCase();
		System.out.println(upper_String);
		String lower_String = a.toLowerCase();
		System.out.println(lower_String);
	}

}

Click "Try It" button to execute above code. The output will be: 

WELCOME TO JAVA
welcome to java

isEmpty() method 

This method returns Boolean value after checking if there anything exists in a String variable. If the variable contains some value (except blank string), it returns false, else it returns true.

Example:

Try It
public class IsEmpty 
{
	public static void main(String[] args) 
	{
		String a = "Welcome To Java";
		System.out.println(a.isEmpty());
		String b= "";
		System.out.println(b.isEmpty());
	}
}

Click "Try It" button to execute above code. The output will be:

false
true

toCharArray() method 

This method takes String as an input and converts it into a character array.

Example:

Try It
public class ToCharArray 
{
	public static void main(String[] args) 
	{
		String a = "Welcome To Java";
		char c[] = a.toCharArray();
		System.out.println(c);
		System.out.println(c[0]);
	}
}

Click "Try It" button to execute above code. The output will be:

Welcome To Java
W

split() method 

split() method splits a String when it encounters a particular string (we can call it delimiter) mentioned by the user.

And, if the delimiter doesn’t exist in the input string, the method will return the input string as the first element of the destination array.

Example:

Try It
public class Split 
{
	public static void main(String[] args) 
	{
		String a = "Welcome To Java";
		String[] b = a.split("o");
		System.out.println(b[0]);
		
		String[] c = a.split("z");
		System.out.println(c[0]);
	}
}

Click "Try It" button to execute above code. The output will be:

Welc
Welcome To Java

endsWith(), startsWith(), contains() method 

As the name suggests, these methods confirms existence of a particular string at a specified position inside a string. The return type is a Boolean (true or false)

endsWith(arg0) – checks if the input string ends with the string provided in arg0.

startsWith(arg0) – checks if the input string starts with the string provided in arg0.

startsWith(arg0, arg1) – checks if the input string starting from index(arg1) with the string provided in arg0.

contains(arg0) – checks if the input string contains the string provided in arg0.

Example:

Try It
public class StringUtility 
{
	public static void main(String[] args) 
	{
		String a = "Welcome To Java";
		String endsWithString = "a";
		System.out.println(a.endsWith(endsWithString));
		String startsWithString = "el";
		System.out.println(a.startsWith(startsWithString));
		System.out.println(a.startsWith(startsWithString, 1));
		String containsString = "sd";
		System.out.println(a.contains(containsString));
	}
}

Click "Try It" button to execute above code. The output will be:

true
false
true
false

indexOf() , lastIndexOf() method 

These methods returns the positions of the characters in a particular string.

int indexOf(int i) -  returns the position of the first occurrence of the character i in the string . If the integer doesn’t exist in the string, it returns -1

int indexOf(int i, int j) - returns the position of the first occurrence of the character i in the substring of the string starting from index j (substring(j)). If the integer doesn’t exist in the string, it returns -1

indexOf(String s) - returns the position of the first occurrence of the string s in the string . If the integer doesn’t exist in the string, it returns -1

indexOf(String s, int i) - returns the position of the first occurrence of the string s in the substring of the string starting from index i (substring(i)). If the integer doesn’t exist in the string, it returns -1

Similarly, int indexOf(int i), int indexOf(int i, int j, indexOf(String s) and indexOf(String s, int i) – all these methods exist, which finds out the position of the last occurrences.

Example:

Try It
public class Index 
{
	public static void main(String[] args) 
	{
		String a = "Welcome To Java 5";
		System.out.println(a.indexOf(1));
		System.out.println(a.indexOf('5', 5));
		System.out.println(a.indexOf("Welcome"));
		System.out.println(a.indexOf("Welcome",10));
		System.out.println(a.lastIndexOf("Wel"));
	}
}

Click "Try It" button to execute above code. The output will be:

-1
16
0
-1
0

replace() , replaceAll(), replaceFirst() method 

replace(char a, char b) – In the input String, this method replaces all the occurrences of char a by char b.

replaceFirst(String strA, String strB) – In the input String, this method replaces the first occurrence of strA a by strB.

replaceAll(String strA, String strB) – In the input String, this method replaces all the occurrences of strA a by strB. The basic difference between replace() and replaceAll() is that, replaceAll() accepts regex and replace() accepts characters.

Example:

Try It
public class Replace 
{
	public static void main(String[] args) 
	{
		String a = "Welcome to Java 5";
		String b = a.replace("e", "X");
		System.out.println(b);
		
		String c = a.replaceFirst("e", "X");
		System.out.println(c);
		
		String d = a.replaceAll("e", "X");
		System.out.println(d);
	}
}

Click "Try It" button to execute above code. The output will be: 

WXlcomX To Java 5
WXlcome To Java 5
WXlcomX To Java 5

equals(), equalsIgnoreCase() method 

All these 2 methods are used for String comparison. Here we need to remember that we cannot compare Strings using == operators. Because == operators are used to compare objects references.

equals() method compares the String case-sensitively

equalsIgnoreCase() method compares the Strings irrespective of their cases.

Example:

Try It
public class Compare 
{
	public static void main(String[] args) 
	{
		String a = "Welcome";
		String b = "Welcome";
		String c = "welcome";
		String d = "compareStrings";
		
		System.out.println(a.equals(b));
		System.out.println(a.equals(c));
		System.out.println(a.equalsIgnoreCase(c));
		System.out.println(a.equalsIgnoreCase(d));
	}
}

Click "Try It" button to execute above code. The output will be: 

true
false
true
false


Next > < Prev
Scroll to Top