Introduction to String Manipulation in Java
String manipulations are very common things for any programming language. Java is a programming language where we can see a huge amount of built-in functions to fulfill our purpose of string manipulations. You can do various things in Java programming like getting the length of a string, finding the character within a string, String concatenation, getting substring, string modification, etc. We can also change the case of a given string; this is also a part of the string manipulation. We have some built-in functions to handle this.
How does it work?
Java programming language comes up with a huge list of string manipulations functions we can use as per our business requirements. We can manipulate a string in many ways.
Given below are the different ways for manipulating a string:
1. String concatenations: Suppose we have two strings in separate. We want these two strings to be concatenated in one. For this, we can simply use the traditional one (the + operator).
String str1= "Hello";
String str2 = "India";
String finalResult = str1+" "+str2;
2. Changing the given string’s case: We can change the case of the string whenever required by using toLowerCase() and the toUpperCase() Java built-in functions.
3. Sub-string inside a string: we have a Contain() method in Java to deal with the checking of the string inside a string. We can use this function to check with some sequence of characters in a defined string. We can also use the substring() of Java.
4. Cleaning the string with the unwanted character: For this, we can use the Java trim() function. This function can be used to remove the space from the beginning and the end of the given string.
5. String reverse: String reverse is again the part of string manipulation. This is all about getting the string from the last index to the first index. For example, we have “Hello world!”, the reverse of this string will be “!dlroW olleH”.
Syntax:
1. toLowerCase()
public String toLowerCase();
This function changes the case of the given string to the lower case. We can use this function at the time of string to handle the ignore case with the compareTo() function itself.
2. toUpperCase()
public String toLowerCase();
This function remains the same as toLowerCase(), but it changes the case to uppercase.
3. Substring()
String substring(int StartIndex, int LastIndex)
In the example code area, we will see the use of this function with the index as an input and the output as well. For this, we need to have a string upon which we will be performing this operation. We can use the position from where we want the string in place of the StartIndex, and we can replace the LastIndex with the end index to where we want the string.
4. trim()
public String trim()
Again, this is a string manipulation function we can use to remove the unwanted space from a given string.
Examples of String Manipulation in Java
Given below are the examples:
Example #1
Using toLowerCase() and compareTo() together.
This is one of the very basic in the Java programming language for the purpose of string comparison. If we will have the same strings, but with different cases, it will not be considered as equal. To handle this with the compareTo() function, we can use the toLowerCase() function to change the case of both strings to the lower before comparison.
Code:
public class StrCompare {
public static void main(String[] args) {
String string1 = "Hello";
String string2 = "hello";
String string3 = string1.toLowerCase();
String string4 = string2.toLowerCase();
int i = string3.compareTo(string4);//0 for both are equal
if(i==0){
System.out.println("Both strings are equal.");
}else{
System.out.print("Strings are not equal.");
}
}
}
Output:
Example #2
String concatenation – We will be using the + operator to handle this.
Code:
public class StrConcat {
public static void main(String[] args) {
String str1= "Hello";
String str2 = "World";
String result = str1+" "+str2;
System.out.print(result);
}
}
Output:
Example #3
Get the length of the string and printing it character by character in reverse order.
After having the length, we can read the string character by character from the string’s last position to the 0 position of the string. In other words, we will see the string reverse of the given string.
Code:
public class StrLenght {
public static void main(String[] args) {
String str1= "Hello World!";
System.out.println("The length of the string: "+ str1.length());
System.out.println("Actual String: "+str1);
System.out.print("String reverse: ");
for(int i = str1.length()-1; i>=0; i--){
System.out.print(str1.charAt(i));
}
}
}
Output:
Example #4
Getting the substring from a given string.
Code:
public class SubString {
public static void main(String[] args) {
String str1= "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
System.out.println(str1.substring(0,10)); // this will give the string from index 0 to 10
System.out.println(str1.substring(10)); // this will give the string from index 10 to the end of the string
}
}
Output:
Conclusion
In Java Programming we can perform various string manipulation using the available tools and the technologies in Java programming. It is better to move ahead with the built-in functions. We have a huge list of the built-in functions to deal with this like – replace(), replaceFirst() ,toLowerCase(), toUpperCase(), split(), substring(), length() functions etc.
Recommended Articles
This is a guide to String Manipulation in Java. Here we discuss the introduction, how does it work and examples. You may also have a look at the following articles to learn more –