String Methods

 

String Methods

 

Here we will see some of the popular methods we can use with strings.

 

length(): This method is used to find the length of a string.


Example:

public class string {
    public static void main(String[] args) {
        String quote = "To be or not to be";
        System.out.println(quote.length());
    }
}


Output:

18

 

indexOf(): This method returns the first occurrence of a specified character or text in a string.


Example:


public class string {
    public static void main(String[] args) {
        String quote = "To be or not to be";
        System.out.println(quote.indexOf("be"));    //index of text
        System.out.println(quote.indexOf("r"));     //index of character
    }
}

Output:

3
7

 

toLowerCase(): Converts string to lower case characters.


Example:

public class string {
    public static void main(String[] args) {
        String quote = "MAXON: Tech and Codes";
        System.out.println(quote.toLowerCase()); 
    }
}

Output:

maxon: tech and codes

 

toUpperCase(): Converts string to upper case characters.


Example:

public class string {
    public static void main(String[] args) {
        String quote = "MAXON: Tech and Codes"  
        System.out.println(quote.toUpperCase());     
    }
}


Output:

MAXON: TECH AND CODES