String Manipulation in Java

String Manipulation” in Java is vital as there can be certain situations where there is a requirement to apply various functionalities to the strings. For instance, concatenating or substring the contained string entries in accordance with the update requirements from time to time. This saves time and manages the current resources effectively at the developer’s end.

This blog will demonstrate “String Manipulation” in Java.

What is “String Manipulation” in Java?

“String Manipulation” in Java refers to manipulating strings. This includes concatenating strings, extracting substrings, replacing characters or substrings, and more. Java provides several built-in classes and methods to perform these operations, such as the “String”, and the “StringBuilder” classes.

How to Create a String?

A string can be created via the following approaches:

Example 1: Creating and Displaying a String in Java

Let’s overview the following example for creating and returning the string:

public class Stringval2 {
 public static void main(String[] args){
  String value1 = "David";
  String value2 = new String("Sara");
  System.out.println("The first string value is -> "+ value1);
  System.out.println("The second string value is -> "+ value2);


In the above code snippet:

  • First of all, create a String via the “String” literal and assign it the stated value.
  • After that, create another String using the “new” keyword such that the String is placed as the “String()” constructor parameter.
  • Lastly, display the created strings one by one.


In this outcome, it can be signified that the strings are created and displayed appropriately.

String Methods in Java

Following are some important String methods in Java.


It gives a new string(substring of the original string).


String substring(int StartIndex, int LastIndex)



This method converts the string to “LowerCase”.


public String toLowerCase();



This method converts the string to “UpperCase”.


public String toUpperCase();


It removes the leading/starting and trailing/ending whitespace from a string.

[cc lang="bash" width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]
public String trim()



It gives “true” if the given string has a “0” length.


public boolean isEmpty()



This method is applied to return the computed string length.


int length()



It is applied to check if the String matches with the specified regular expression(regex) or not.


boolean matches(String regex)


Example 2: Applying the String “concat()” and “length()” Methods to Concatenate and Return the String Length in Java

This example applies the discussed String methods to concatenate the initialized string and return the length of the concatenated string, respectively:

public class Stringlength {
 public static void main(String[] args){
  String string1 = "Linux";
  String string2 = "hint";
  String string3 = string1.concat(string2);
  System.out.println("After concatenating(concat Method) the strings -> "+  string3);
  System.out.println("After concatenating(+ Operator) the strings -> "+ string1 +  string2);
  System.out.println("The length of the concatenated string is: "+ string3.length());


According to the above lines of code, apply the following steps:

  • First of all, initialize the stated String values.
  • In the next step, associate the “concat()” method to concatenate the initialized strings and display the concatenated string.
  • Alternatively, the “+” operator is also applied to concatenate the strings.
  • Lastly, apply the “length()” method to compute the concatenated string’s length.


In this outcome, it can be seen that the initialized string values are concatenated and the corresponding string length is also returned.

Example 3: Applying the String “substring()” Method to Substring the String in Java

In this particular example, the discussed method can be implemented to substring the string by referring to the start and end indexes and only the start index as well:

public class String3 {
 public static void main(String[] args) {
  String string = "JavaProgramming";


In the above code snippet:

  • Likewise, initialize the String value.
  • After that, apply the “substring()” method twice by referring to the “start(0)” and “end(4)” indexes in the string and only the start index(11), respectively.
  • Finally, display the resultant substrings in both cases.


This output implies that the string is a substring in accordance with the specified indexes.


String manipulation” refers to manipulating strings in the Java programming language. This includes concatenating strings, extracting substrings, etc. This article manipulated the string by concatenation, computing its(string) length, and retrieving the substring from a string via the String methods.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.