Master java skills

String Methods

There are several operations that can be performed on String objects. In this chapter, we will learn about important ones.

Length of a String

public int length()

length of any string can be found by calling length() method on the string object.

package com.javatrainingschool;

public class StringLengthExample {
	
	public static void main(String[] args) {
		
		String s1 = "Arjun Kumar Singh";
		System.out.println("s1 length : " + s1.length());
		
	}
}
Output:

s1 length : 17

Note -> Spaces between different words in a String are also counted by length() method.

Concatenation of Strings

concat() method is used to concatenate two string objects.

package com.javatrainingschool;

public class StringConcatenationExample {
	
	public static void main(String[] args) {
		
		String s1 = "Arjun";
		String s2 = "Kumar";
		
		String s3 = s1.concat(s2);
		System.out.println(s3);
		
	}
}
Output :
ArjunKumar

Concatenation of two string objects can also be done by simply putting a + operator between them.

String concatenation using + operator

package com.javatrainingschool;

public class StringConcatenationExample {
	
	public static void main(String[] args) {
		
		String s1 = "Arjun";
		String s2 = "Kumar";
		
		String s3 = s1 + " " + s2;
		System.out.println(s3);
		
	}
}
Output:
Arjun Kumar

Substring in java

A certain part of a String is known as substring. It is subset of the main string.

For example, ‘Ram’ is substring of string ‘Ram Kumar’. Similarly, ‘Ram K’, ‘Ram Ku’, ‘am Kum’, ‘m Kumar’, all of these are substrings of the string ‘Ram Kumar’.

substring() method can be used to get substring. There are two overloaded versions of substring() method

public String substring(int startIndex)                         //startIndex inclusive
public String substring(int startIndex, int endIndex)          //endIndex exclusive

Java substring example with startIndex

package com.javatrainingschool;

public class StringLengthExample {
	
	public static void main(String[] args) {
		
		String s1 = "Arjun Kumar";
		
		String s2 = s1.substring(3);
		System.out.println(s2);
		
	}
}
Output:
un Kumar

Java substring example with start and end Index

package com.javatrainingschool;

public class StringLengthExample {
	
	public static void main(String[] args) {
		
		String s1 = "Arjun Kumar";
		
		String s2 = s1.substring(2, 8);
		System.out.println(s2);
		
	}
}
Output:
jun Ku

Below table shows other important methods of String class

SrMethodDescription
1charAtGives the character at the specified index
2indexOfGives the index of the specified character
3containsUsed to check if a particular character/charSequence is contained within the String or not
4equalsChecks if the two strings being compared are equal or not
5equalsIgnoreCaseChecks if the two strings are equal or not ignoring their cases
6isEmptyChecks whether a string is empty or not
7endsWithChecks whether a string ends with the specified character sequence or not
8startsWithChecks whether a string begins with the specified character sequence or not
9replaceReplaces existing char/charSequence with the specified char/charSequence
10toCharArrayConverts the String into a character array