Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Strings?
3.
What is string manipulation in Java?  
4.
Why do we use Strings?
5.
How to create a String?
5.1.
String Literal 
5.2.
Using new keyword
6.
String Syntax Examples
6.1.
Java
6.2.
Java
7.
Various Functions for String Manipulation
7.1.
String concatenations
7.2.
substring()
7.3.
toLowerCase()
7.4.
toUpperCase()
7.5.
trim()
8.
Examples of String Manipulation
8.1.
Example 1
8.2.
Implementation
8.3.
Java
8.4.
Example 2
8.5.
Implementation
8.6.
Java
8.7.
Example 3
8.8.
Implementation
8.9.
Java
8.10.
Example 4
8.11.
Implementation
8.12.
Java
9.
Frequently Asked Questions
9.1.
Can strings be manipulated in Java?
9.2.
What are the 4 string manipulation functions?
9.3.
What are the types of string manipulation?
9.4.
What are the string 5 methods?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

String Manipulation in Java

Introduction

String manipulation in Java refers to the process of modifying or working with strings, which are sequences of characters. This includes tasks like concatenating strings, extracting substrings, searching for specific characters or patterns, and replacing parts of strings. String manipulation is a fundamental aspect of Java programming used in various applications for data processing and manipulation.

string manipulation in java

The article explains the details of string manipulation in Java. Let's get started.

What are Strings?

Every group of characters a script interprets literally is known as a string. Strings include things like "CodingNinjas" and "CODING1234". Alternatively, we may define a string as a collection of characters, including letters, numbers, symbols, and spaces. For it to be recognized as a string, it must be encapsulated in quotation marks.

For example: str="CodingNinjas";

Here the variable is assigned a value which is CodingNinjas.

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

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

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

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;

substring()

Gives a new string that is a substring of the original string.

Syntax

String substring(int StartIndex, int LastIndex)

toLowerCase()

This function is the same as toLowerCase(), but it converts the case to uppercase.

Syntax

public String toLowerCase();

toUpperCase()

This function being the same as toUpperCase(), but it converts the case to Lowercase.

Syntax

public String toUpperCase();

trim()

Removes leading and trailing whitespace from a string.

Syntax

public String trim()

You can also read about the topic of Java Destructor and Java Ioexception

Examples of String Manipulation

Here are some common examples of string manipulation in Java.

Example 1

We use 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

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

output

Example 2

Using the + operator, we will handle string concatenation.

Implementation

  • Java

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

output

Example 3

Getting the substring from a given string.

Implementation

  • Java

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

Output

Example 4

Using trim()

Implementation

  • Java

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

Output

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

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: 

To learn more about Data Structures and Algorithms, you can enroll in our course on DSA in Java. Do upvote our blogs to help other ninjas grow. 

Happy Coding 

Live masterclass