String Functions in Java


☞These functions deal only with string manipulations.

☞Some of them are :

1. length()
2. charAt()
3. indexOf()
4. lastIndexOf()
5. substring()
6. toUpperCase()
7. toLowerCase()
8. replace()
9. trim()
10.endsWith()
11.startsWith()

length()

☞It is used to return the length of a string i.e. number of characters in the string.

☞Function prototype : int length()

☞It returns a value of int type.

☞Syntax : <string or string_variable>.length();

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = "inspirewebsoft";
        System.out.println("_inspirewebsoft_".length());
        System.out.println(s1.length());
    
    }
}

Output

16
14

charAt()

☞It is used to return a character from the given index of the string.

☞Function prototype : char charAt(int index)

☞It returns a value of char type.

☞Syntax : <string or string_variable>.charAt(index_number);

Example

public class StringExample {
public static void main(String[] args) {

String s1 = "inspirewebsoft";
System.out.println("_inspirewebsoft_".charAt(15));
System.out.println(s1.charAt(6));

}
}

Output

_
e

indexOf(char ch)

☞It is used to return the length of a string i.e. number of characters in the string.

☞Function prototype : int indexOf(char ch) or int indexOf(char ch, int begin_index)

☞It returns a value of int type.

☞Syntax : <string or string_variable>.indexOf(character,begin_index);

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = "inspirewebsoft";
        System.out.println("_inspirewebsoft_".indexOf('_'));
        System.out.println(s1.indexOf('e'));
        System.out.println(s1.indexOf('e',7));
    
    }
}

Output

0
6
8

lastIndexOf()

☞It is used to return the index of the last occurence of a character in the string.

☞Function prototype : int lastIndexOf(char ch)

☞It returns a value of int type.

☞Syntax : <string or string_variable>.lastIndexOf(character);

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = "inspirewebsoft";
        System.out.println("_inspirewebsoft_".lastIndexOf('_'));
        System.out.println(s1.lastIndexOf('e'));
    
    }
}

Output

15
8

substring()

☞Part of a string is known as substring.

☞It is used to extract a set of characters simultaneously from a given index up to the end of string or to the index number specified.

☞Function prototype : String substring(int begin_index) or String substring(int begin_index, int last_index)

☞It returns a value of string type.

☞Syntax : <string or string_variable>.substring(begin_index); or <string or string_variable>.substring(begin_index,last_index);

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = "inspirewebsoft";
        System.out.println("_inspirewebsoft_".substring(7));
        System.out.println(s1.substring(4));
        System.out.println(s1.substring(4,10));
    
    }
}

Output

ewebsoft_
irewebsoft
ireweb
Note : begin_index is inclusive whereas last_index is exclusive

toLowerCase()

☞It is used to convert a given string into lowercase characters.

☞Function prototype : String toLowerCase()

☞It returns a value of String type.

☞Syntax : <string or string_variable>.toLowerCase();

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = "inspirewebSOFT";
        System.out.println("_inspirewebsoft_".toLowerCase());
        System.out.println(s1.toLowerCase());
    
    }
}

Output

_inspirewebsoft_
inspirewebsoft
Note : If character is already in lower case or is a special character, then it will remain same.

toUpperCase()

☞It is used to convert a given string into uppercase characters.

☞Function prototype : String toUpperCase()

☞It returns a value of String type.

☞Syntax : <string or string_variable>.toUpperCase();

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = "inspirewebSOFT";
        System.out.println("_inspirewebsoft_".toUpperCase());
        System.out.println(s1.toUpperCase());
    
    }
}

Output

_INSPIREWEBSOFT_
INSPIREWEBSOFT
Note : If character is already in upper case or is a special character, then it will remain same.

replace()

☞It is used to replace a character by another character or to replace a String by another String at all its occurences in the given string.

☞Function prototype : String replace(char old_char, char new_char) or String replace(String old_string, String new_string)

☞It returns a value of String type.

☞Syntax : <string or string_variable>.replace(old_character, new_character); or <string or string_variable>.replace(old_string, new_string);

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = "inspirewebSOFT";
        System.out.println("_inspirewebsoft_".replace('e','*'));
        System.out.println(s1.replace("SOFT","soft"));
        
    }
}

Output

_inspir*w*bsoft_
inspirewebsoft

trim()

☞It is used to remove leading and trailing blanks from a string.

☞Function prototype : String trim()

☞It returns a value of String type.

☞Syntax : <string or string_variable>.trim();

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = " We develop a passion for learning ";
        System.out.println(" inspirewebsoft ".trim());
        System.out.println(s1.trim());
        
    }
}

Output

inspirewebsoft
We develop a passion for learning
Note : The other blanks, which are available in between the words will remain unchanged.

endsWith()

☞It is used to check whether a given string has a specified suffix or not.

☞Function prototype : boolean endsWith(String s)

☞It returns a value of boolean type.

☞Syntax : <string or string_variable>.endsWith(string);

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = " We develop a passion for learning ";
		System.out.println("inspirewebsoft".endsWith("soft"));
		System.out.println(s1.endsWith("We"));
        
    }
}

Output

true
false

startsWith()

☞It is used to check whether a given string has a specified prefix or not.

☞Function prototype : boolean startsWith(String s)

☞It returns a value of boolean type.

☞Syntax : <string or string_variable>.startsWith(string);

Example

public class StringExample {
    public static void main(String[] args) {
        
        String s1 = " We develop a passion for learning ";
		System.out.println("inspirewebsoft".startsWith("soft"));
		System.out.println(s1.startsWith("We"));
        
    }
}

Output

false
true