Commonly used String Operations in Java

Strings are widely used in Java programming. There is no single package or class in our project that are not using strigs. The Java platform provides the String class to create and manipulate strings.

Creating a String

We can directly create a string from literals assigned to String objects.

String str="Hello";

Create a string from a character array

char[] helloArray = { 'H', 'e', 'l', 'l', 'o', ' ','W', 'o', 'r', 'l', 'd', '!'}; String str = new String(helloArray); System.out.println( str );

Get String length

Many times we need to calculate the string length. We need to find how many characters String contains. String class has a length method which will return the length of the String.

String str = "Hello World !!!"; int len = str.length(); System.out.println( "String Length is : " + len );

Convert Char into a String

We can convert single char to String as below.

String.valueOf(c)

Similarly, we can convert any kind of data type into String using this valueOf() of String class.

String boolString = String.valueOf(boolean b); String floatString = String.valueOf(float b) String longString = String.valueOf(long l) String doubleString = String.valueOf(double d)

etc.

Appending Strings

We can append the new string to the existing string using the append method of StringBuilder or StringBuffer class.

String newStr = new StringBuilder("Hello").append("world!!").toString();

Get a Character by Index

We can get any character from string using the charAt() method of String class.

String str = "HelloWorld!!" char ch = str.charAt(5); // return W

Concatenation of String

We can combine multiple Strings using String concatenation. We can use + operator or we can use StringBuffer or StringBuilder class to concat multiple Strings into a single String.

String str = "I am good Programmer"; System.out.println("Hello,"+str+"in Java.");//Hello,I am good Programmer in Java.

Using StringBuffer

StringBufer sb = new StringBuffer("Hello"); sb.append("I am good Programmer"); sb.append("in"); sb.append("Java"); sb.append(".");

When converting response JSON in many java applications into a Java object format or our required format we have to use the StringBuffer class for concatenation.

Format String

We can replace or format string using the format method of the String class. In many applications, we have predefined strings in property files and we need to replace the values to new values. In this case, we have to use the format method of the String class.

String str = "I am %s Java programmer."; Srting newStr = String.fornat(str,"Expert"); //I am Expert Java programmer.

Here %s is formatted to Expert.

Split the String

Many times we need to split the string based on given delimiters like,: or space

String message = "Hello World"; String[] split = message.split(" "); [Hello World]

Removing All Whitespace

When we get inputs from users using applications,inputs may contain spaces. To remove leading and trailing whitespaces String has a trim() method to remove all whitespaces.

String str =" HelloWorld "; String newstr = str.trim(); //HelloWorld

How to find the character in string in java?

for(i=0: i=str. length() - 1: i++) { Char ch = str.charAt(i) : System. out. Println(ch) : }


Post/Questions related to commonly used String Operations in Java

How to use string functions in java?
How to get specific string from a string in java?
How to read each character of a string in java?
InputStream to String Conversion – example
String vs StringBuffer
Convert String to boolean primitive
boolean to String conversion
Convert String object to Boolean object
Remove trailing spaces of a String
Left pad a String with zeros/spaces
Right pad a String with zeros/spaces
Program to find duplicate characters in a String
Convert char to String and vice versa
Convert String to int in Java
Convert int to String
Convert String to Double in Java
Double to String conversion
Convert String to Long
Long to String conversion
Convert a char array to String
String to Date conversion
Date to String conversion
StackTrace to String conversion
The writer to String conversion
Convert String to ArrayList
Java 8 – StringJoiner Example
ASCII to String conversion
float to String conversion
Program to reverse words of a String in Java
Program to reverse a String in Java using Recursion
How to get the first index of the string in java?

In this article, we have seen the  Commonly used String Operations in Java with examples. All source code in the article can be found in the GitHub repository.