Hey, guys in this post will discuss the most commonly used java string methods. The string is the most commonly used class while building any java application, this is why the java team has provided several built-in String methods that we can do on any String. Let’s discuss some of the most commonly used java string methods.
- chartAt()
- compareTo()
- concat()
- contains()
- endsWith()
- equals()
- indexOf()
- isEmpty()
- length()
- replace()
- startsWith()
- trim()
- toLowerCase()
- toUpperCase()
Table of Contents
Usage of charAt()
It returns the character at the specified index in a string. If the specified index is greater than the length of the string then it will throw StringIndexOutOfBoundException
.
public class Test {
public static void main(String[] args) {
System.out.println("Hello world".charAt(2));//l
System.out.println("javatutorial".charAt(0));//j
}
}
Further read on
charAt()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int)
Usage of compareTo()
It compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.
public class Test {
public static void main(String[] args) {
System.out.println("bb".compareTo("dd")); //-2
System.out.println("dd".compareTo("bb")); //2
System.out.println("cc".compareTo("cc")); //0
}
}
Further read on
compareTo()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)
Usage of concat()
Appends a string to the end of another string. If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
public class Test {
public static void main(String[] args) {
System.out.println("bushan".concat(" sirgur")); //bushan sirgur
System.out.println("hello".concat(" world")); //hello world
System.out.println("b2".concat(" tech")); //b2 tech
}
}
Further read on
concat()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#concat(java.lang.String)
Usage of contains()
checks whether a string contains a sequence of characters.
public class Test {
public static void main(String[] args) {
System.out.println("b2 tech".contains("tec"));//true
System.out.println("javatutorial".contains("tech"));//false
}
}
Further read on
contains()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
Usage of endswith()
checks whether a string ends with a specified character or not
public class Test {
public static void main(String[] args) {
System.out.println("b2 tech".endsWith("ch"));//true
System.out.println("javatutorial".endsWith("tech"));//false
}
}
Further read on
endsWith()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String)
Usage of equals()
returns true if two string are equal otherwise returns false
public class Test {
public static void main(String[] args) {
System.out.println("b2 tech".equals("b2 tech"));//true
System.out.println("javatutorial".endsWith("hello"));//false
}
}
Further read on
equals()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)
Usage of indexOf()
returns the position of the first found occurrence of specified characters, otherwise returns -1
public class Test {
public static void main(String[] args) {
System.out.println("b2 tech".indexOf("tech")); //3
System.out.println("javatutorial".indexOf("hello")); //-1
}
}
Further read on
indexOf()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)
Usage of isEmpty()
checks whether a string is empty or not
public class Test {
public static void main(String[] args) {
System.out.println("".isEmpty()); //true
System.out.println("b2 tech".isEmpty()); //false
}
}
Further read on
isEmpty()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty()
Usage of length()
returns the length of the specified string. index starts from 0 index.
public class Test {
public static void main(String[] args) {
System.out.println("".length()); //0
System.out.println("b2 tech".length()); //7
}
}
Further read on
length()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#length()
Usage of replace()
returns a new string where the specified values are replaced otherwise returns the same string
public class Test {
public static void main(String[] args) {
System.out.println("hello world".replace("world", "java")); //hello java
System.out.println("b2 tech".replace("world", "java")); //b2 tech
}
}
Further read on
replace()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char,%20char)
Usage of startsWith()
checks whether a string starts with a specified character or not
public class Test {
public static void main(String[] args) {
System.out.println("hello world".startsWith("hello")); //true
System.out.println("b2 tech".startsWith("java")); //false
}
}
Further read on
startsWith()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith(java.lang.String,%20int)
Usage of trim()
removes whitespace from both ends of the string
public class Test {
public static void main(String[] args) {
System.out.println(" hello world ".trim()); //hello world
System.out.println(" b2 tech".trim()); //b2 tech
}
}
[irp]Further read on
trim()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
Usage of toLowerCase()
convert specified string to lower case
public class Test {
public static void main(String[] args) {
System.out.println("HELLO WORLD".toLowerCase()); //hello world
System.out.println("b2 tech".toLowerCase()); //b2 tech
}
}
Further read on
toLowerCase()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toLowerCase()
Usage of toUpperCase()
convert the specified string to upper case.
public class Test {
public static void main(String[] args) {
System.out.println("hello world".toUpperCase()); //HELLO WORLD
System.out.println("b2 tech".toUpperCase()); //B2 TECH
}
}
Further read on
toUpperCase()
at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toUpperCase(java.util.Locale)