What is String Manipulation in Java?
String manipulation refers to manipulating strings in the Java programming language.
This includes concatenating strings, extracting substrings, replacing characters or substrings, and more. Java provides several built-in classes and methods for performing these operations, such as the String class and the StringBuilder class. Additionally, regular expressions can be used to perform more complex string manipulations.
Why do we use Strings?
As we are already aware that strings are collections of characters, including letters, numbers, symbols, and spaces. Strings are among the most commonly used data types in all programming languages. Strings are used to store texts and characters. It is used to show a set of characters which includes characters, spaces, and numbers. In Java, strings are objects. Java provides a String class to create and manipulate strings.
How to Create a String?
In Java, strings are considered objects. Java provides a String class to perform various string operations. In Java, string objects are created by two means. The first one is by using a string literal, and the second one involves using the new keyword.
String Literal
Strings are created using double quotes. Double quotations surround a string of characters. For example:
String str="CodingNinjas";
Using new keyword
Here, the strings are created using the keyword new. For example:
String str=new String(“CodingNinjas”);
In this scenario, the literal "CodingNinjas" will be added to the string constant pool, and the JVM will create a new string object in regular (non-pool) heap memory. The variable str (non-pool) will identify the object in a heap.
String Syntax Examples
Let us see some string syntax examples in Java.
Example 1:
Java
class Main {
public static void main(String[] args) {
// creation of strings using String literals
String str = "Coding Ninjas Studio";
String str1 = "Coding";
String str2 = "Ninjas";
// print the strings on the screen
System.out.println(str);
System.out.println(str1);
System.out.println(str2);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Coding Ninjas Studio
CodingNinjas
Example 2:
Java
class Main {
public static void main(String[] args) {
// creation of strings using new keyword
String str = new String("Coding Ninjas Studio");
String str1 = new String("Coding");
String str2 = new String("Ninjas");
// print the strings on the screen
System.out.println(str);
System.out.println(str1);
System.out.println(str2);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Coding Ninjas Studio
CodingNinjas
Various Functions for String Manipulation
Some common string manipulation methods available in the Java String class include:
- String concatenations
- substring()
- toLowerCase()
- toUpperCase()
- trim()
1. String concatenations
Let's say we have two different strings. These two strings should be combined into one. We can use the standard one (the + operator) for this.
Syntax
String str1= "Coding";
String str2 = "Ninjas";
String result = str1+" "+str2;
2. substring()
Gives a new string that is a substring of the original string.
Syntax
String substring(int StartIndex, int LastIndex)
3. toLowerCase()
This function is the same as toLowerCase(), but it converts the case to uppercase.
Syntax
public String toLowerCase();
4. toUpperCase()
This function being the same as toUpperCase(), but it converts the case to Lowercase.
Syntax
public String toUpperCase();
5. trim()
trim() function removes leading and trailing whitespace from a string.
Syntax
public String trim()
Examples of String Manipulation
Here are some common examples of string manipulation in Java.
Example 1 - Using toLowerCase() and compareTo() together
This is one of the most fundamental Java programming concepts for string comparison. If we use the same strings but different cases, it won't be regarded as being equal. We can use the toLowerCase() function to lowercase both texts before comparing them to manage this with the compareTo() function.
Implementation
Java
public class Main
{
public static void main(String[] args) {
String string1 = "Ninjas";
String string2 = "ninjas";
String string3 = string1.toLowerCase();
String string4 = string2.toLowerCase();
int i = string3.compareTo(string4);
if(i==0){
System.out.println("Both strings are equal.");
}
else{
System.out.print("Strings are not equal.");
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Both strings are equal.
Example 2 - Using the + operator, we will handle string concatenation.
Implementation
Java
public class Main
{
public static void main(String[] args) {
String str1= "Coding";
String str2 = "Ninjas";
String result = str1+" "+str2;
System.out.print(result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Coding Ninjas
Example 3 - Getting the substring from a given string
Implementation
Java
public class Main
{
public static void main(String[] args) {
String str1= "Welcome to coding ninjas.";
System.out.println(str1.substring(0,11)); // this will give the string from index 0 to 11
System.out.println(str1.substring(11));
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Welcome to
coding ninjas.
Example 4 - Using trim() Method
Implementation
Java
public class Main
{
public static void main(String[] args) {
String original = " Coding Ninjas! ";
System.out.println(original);
System.out.println(original.trim());
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Coding Ninjas!
Coding Ninjas!
Also see, Palindrome string
Frequently Asked Questions
Can strings be manipulated in Java?
Since the Java String is immutable, it cannot be changed. String manipulation includes concatenating strings, extracting substrings, replacing characters or substrings, and more.
What are the 4 string manipulation functions?
The 4 string manipulation functions in Java are – substring(), toLowerCase(), toUpperCase() and trim(). These functions allows you to perform different types of string manipulations in Java such as character case conversion, removal of white spaces, and substring extraction.
What are the types of string manipulation?
A method that derives from the String class itself and doesn't need an instance of that class to function is referred to as a shared method. Instance methods must be qualified with the instance name because they are descended from a specific instance of String.
What are the string 5 methods?
An assigned character is returned by the function charAt(). With equals(), two strings are compared. Concatenates two strings by adding one to the end of the other using concat(). A string's length is returned using the length() function. Lowercase letters are created using toLowerCase().
Conclusion
String manipulation in Java is a versatile and essential aspect of programming. It enables developers to efficiently process, modify, and analyze text data using built-in methods like concatenation, substring extraction, and case conversion. In this article, we have extensively discussed the details and Methods of string manipulation in Java.
We hope the blog has helped you enhance your knowledge of string manipulation in Java. For more about Java, you can refer to these articles: