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
Sr | Method | Description |
---|---|---|
1 | charAt | Gives the character at the specified index |
2 | indexOf | Gives the index of the specified character |
3 | contains | Used to check if a particular character/charSequence is contained within the String or not |
4 | equals | Checks if the two strings being compared are equal or not |
5 | equalsIgnoreCase | Checks if the two strings are equal or not ignoring their cases |
6 | isEmpty | Checks whether a string is empty or not |
7 | endsWith | Checks whether a string ends with the specified character sequence or not |
8 | startsWith | Checks whether a string begins with the specified character sequence or not |
9 | replace | Replaces existing char/charSequence with the specified char/charSequence |
10 | toCharArray | Converts the String into a character array |