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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

Explanation: The above program prints the characters of different works present in the input string on the basis of their indices.

Pin It on Pinterest

Share This