String Manipulation in Java
String Manipulation in Java
In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of string includes getting the length of a string, finding the character within a string, String concatenation, getting substring, string modification, etc. The string manipulation can be done manually by a developer or programmer. However, doing things manually takes time. Therefore, Java provides a number of predefined methods to do the manipulation of string. A few of those predefined methods are:
- split()
- trim()
- toLowerCase()
- toUpperCase()
- subSquence()
Let’s discuss it in detail.
Java String.split() Method
The split() method takes a regular expression as its parameter and returns an array of strings.
FileName: SplitMethodExample.java
// A basic Java program that shows the working of the split() method. public class SplitMethodExample { // main method public static void main(String argvs[]) { // creating an object of String String str = new String("Tutorial & Example"); // the split() method returns an array of string String arrStr[] = str.split(" "); // size of the array int size = arrStr.length; // iterating over the element of the string array for(int i = 0 ; i < size; i++) { System.out.println(arrStr[i]); } } }
Output:
Tutorial & Example
Explanation: In the above program, the split() method splits the input string on the basis of white spaces present in the string. As the input string contains two white spaces, therefore, whatever is present between the index 0 and the first white space is treated as the first element of the resultant string array. Characters from the first white space to the second white space form the second element, and the characters from the second white space till the end of the string form the third element of the resultant string array.
Java String.trim() Method
The trim() method takes no parameter and returns a string. The trim() method removes the trailing and leading spaces from the given string.
FileName: TrimMethodExample.java
// A basic Java program that shows the working of the trim() method. public class SplitMethodExample { // main method public static void main(String argvs[]) { // The input string contains two leading spaces and two trailing spaces String str = new String(" Tutorial & Example "); System.out.println("The given string is: " + str + "\n"); // invoking the trim() method str = str.trim(); System.out.println("The given string is: " + str); } }
Output:
The given string is: Tutorial & Example The given string is: Tutorial & Example
Explanation: The leading and trailing spaces have been removed, and the same is evident by looking at the output.
Java String.toLowerCase() Method
The toLowerCase() method does not accepts any parameter and returns a string. The toLowerCase() method converts the given string into the lower case.
FileName: ToLowerCaseMethodExample.java
// A basic Java program that shows the working of the toLowerCase() method. public class ToLowerCaseMethodExample { // main method public static void main(String argvs[]) { // The input string contains two leading spaces and two trailing spaces String str = new String("Tutorial & Example"); System.out.println("The given string is: " + str + "\n"); // invoking the toLowerCase() method str = str.toLowerCase(); System.out.println("The given string is: " + str); } }
Output:
The given string is: Tutorial & Example The given string is: tutorial & example
Explanation: Observe that letter ‘T’ in ‘Tutorial’ and ‘E’ in ‘Example’ have been converted into ‘t’ and ‘e’, respectively.
Java String.toUpperCase() Method
The toUpperCase() method takes no parameter and returns a string. The toUpperCase() method converts the given string into the upper case.
FileName: ToUpperCaseMethodExample.java
// A basic Java program that shows the working of the toUpperCase() method. public class ToUpperCaseMethodExample { // main method public static void main(String argvs[]) { // The input string contains two leading spaces and two trailing spaces String str = new String("Tutorial & Example"); System.out.println("The given string is: " + str + "\n"); // invoking the toUpperCase() method str = str.toUpperCase(); System.out.println("The given string is: " + str); } }
Output:
The given string is: Tutorial & Example The given string is: TUTORIAL & EXAMPLE
Explanation: Observe that every letter of the words ‘Tutorial’ as well as ‘Example’ has been converted into upper case.
Java String.subSequence() Method
The subSequence() method takes two arguments. One is the startIndex and other is the endIndex. Whatever characters are present between the startIndex and endIndex, startIndex inclusive and endIndex exclusive ([startIndex, endIndex)), are returned. The return type of the method subSquence() is CharSequence.
FileName: SubSequenceMethodExample.java
// A basic Java program that shows the working of the subSequence() method. public class SubSequenceMethodExample { // main method public static void main(String argvs[]) { // The input string String str = "Hello and Welcome to Tutorial & Example "; System.out.println("The given string is: " + str + "\n"); int startIndex = 0; int endIndex = 5; // invoking the subSequence() method CharSequence cs = str.subSequence(startIndex, endIndex); System.out.println("Subsequence from index "+ startIndex + " to " + endIndex + " is: " + cs); // Updating the startIndex and endIndex startIndex = 10; endIndex = 17; // indices from 10 to 16 are considered cs = str.subSequence(startIndex, endIndex); System.out.println("Subsequence from index "+ startIndex + " to " + endIndex + " is: " + cs); // Updating the startIndex and endIndex startIndex = 21; endIndex = 29; // indices from 21 to 28 are considered cs = str.subSequence(startIndex, endIndex); System.out.println("Subsequence from index "+ startIndex + " to " + endIndex + " is: " + cs); } }
Output:
The given string is: Hello and Welcome to Tutorial & Example Subsequence from index 0 to 5 is: Hello Subsequence from index 10 to 17 is: Welcome Subsequence from index 21 to 29 is: Tutorial
Explanation: The above program prints the characters of different works present in the input string on the basis of their indices.