String

Talks about String

String is a sequence of characters. In java, java String is an object.

There are 3 types of String classes:

  1. String Class

  2. String Buffer Class

  3. String Builder Class

String Class:

String s1 = "Al Amin Khan";
String s2 = new String("Al Amin Khan");

input: 
    s1 == s2 
output: 
    false    //Because here s1 and s2 are different objects.


input: 
    s1.equals(s2) 
output: 
    True    //Because here s1 and s2 compare the string of the objects.
    
input: 
    s1.contains("Khan") //check if the string exists in the original string. 
output: 
    true 
    

ChartAt(): return the character value of the index of the string.

input:
    char ch = s1.charAt(0);
    System.out.println(ch);
output:
    A

Trim, Strip and Split:

  • strip(): will remove white space only from the beginning of the string.

  • trim(): will remove white spaces from the start and from the ending. But not in between.

  • split(): will return an array of strings and split can be done with specific requirements such as white space, specific later or character.

input:
    "  Al    Amin Khan   ".trim();
output:
    Al    Amin Khan
String[] values = "[email protected]".split("@");
for(String value : values){
    System.out.println(value);
}

Last updated