String Functions – String class from package java.lang have some methods which can manipulate string literals.
String1.concat(String1) – This function used for joining two strings together. This can be done by: String1=String1+
String2.
Example:
String Str=“Hello”;
Str=Str.concat(“ Java”); // or Str=Str + “Java” //Output Hello Java
String.trim() - Removes white space from both ends of this string.
Example:
String Str=“ Hello ”;
System.out.println(Str.trim()); // Output -"Hello"
String.indexOf(char/string) - Returns the index within the string of the first occurrence of
the specified character, if search fail then it will return -1.
Example:
String Str= “I live in India. I love
my country India”
int
L=Str.indexOf(“India”); // Output - > 10 [ First occurrence of ‘India’ ]
String.indexOf(char/string, int) - Returns the index within this string of the first occurrence of
the specified character, starting from the specified index, if search fail then
it will return -1.
Example:
String Str= “I live in India. I love
my country India”
int
L=Str.indexOf(“India”,12); // Output - > 35 [ Occurrence of ‘India’ after 12th
Postion ]
String.lastIndexof(char/String) - Returns the index within the string of the last occurrence of the
specified character, if search fail then it will return -1.
lastIndexOf(String Str, int StartIndex)
int lastIndexOf(String str, int StartIndex): Returns the last index position of specified string starting from start Index.
Example:
String Str="This is a demo of lastIndexOf";
System.out.println(lastIndexOf('i', 3)); // Return 2.
lastIndexOf(String Str, int StartIndex)
int lastIndexOf(String str, int StartIndex): Returns the last index position of specified string starting from start Index.
Example:
String Str="This is a demo of lastIndexOf";
System.out.println(lastIndexOf('i', 3)); // Return 2.
Example:
String Str= “I live in India. I love
my country India”
int
L=Str.lastIndexOf(“India”); // Output - > 35 [ last occurrence of ‘India’ ]
String.firstIndexof(char/String) - Returns the index within the string of the first occurrence of
the specified character, if search fail then it will return -1.
Example:
String Str= “I live in India. I love
my country India”
int
L=Str.indexOf(“India”); // Output - > 10 [ First occurrence of ‘India’ ]
String.startswith(String) -Tests if this string starts with the specified prefix and return a
Boolean value.
Example:
System.out.println(“Hello Friends”.startsWith(“A”); // Return ‘false’
String.endsWith(String) - Tests if this string ends with the specified suffix and return a
Boolean value.
Example:
System.out.println(“Hello
Friends.”.startsWith(“.”); // Return
‘true’
String.replace(char, char) or
String.replace(String, String) - Returns a new string resulting from
replacing all occurrences of oldChar in this string with newChar
or oldString with the newString.
Example:
String test=”You is too good”;
test=rest.replace(“is”,”are”); // New value
of tes=”You are too good”
String.toLowerCase() / String.toUpperCase() - Converts the String
to lowercase / Converts the String to upper case.
Example:
System.out.println(“Hello
Friends”.toUpperCase(); // “HELLO
FRIENDS”
System.out.println(“Hello
Friends”.toLowerCase(); // “hello friends”