☞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()
☞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();
public class StringExample { public static void main(String[] args) { String s1 = "inspirewebsoft"; System.out.println("_inspirewebsoft_".length()); System.out.println(s1.length()); } }
16 14
☞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);
public class StringExample { public static void main(String[] args) { String s1 = "inspirewebsoft"; System.out.println("_inspirewebsoft_".charAt(15)); System.out.println(s1.charAt(6)); } }
_ e
☞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);
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)); } }
0 6 8
☞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);
public class StringExample { public static void main(String[] args) { String s1 = "inspirewebsoft"; System.out.println("_inspirewebsoft_".lastIndexOf('_')); System.out.println(s1.lastIndexOf('e')); } }
15 8
☞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);
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)); } }
ewebsoft_ irewebsoft ireweb
☞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();
public class StringExample { public static void main(String[] args) { String s1 = "inspirewebSOFT"; System.out.println("_inspirewebsoft_".toLowerCase()); System.out.println(s1.toLowerCase()); } }
_inspirewebsoft_ inspirewebsoft
☞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();
public class StringExample { public static void main(String[] args) { String s1 = "inspirewebSOFT"; System.out.println("_inspirewebsoft_".toUpperCase()); System.out.println(s1.toUpperCase()); } }
_INSPIREWEBSOFT_ INSPIREWEBSOFT
☞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);
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")); } }
_inspir*w*bsoft_ inspirewebsoft
☞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();
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()); } }
inspirewebsoft We develop a passion for learning
☞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);
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")); } }
true false
☞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);
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")); } }
false true