How to remove a specific character in a String in Java?

Requirement:

Remove specific character in a string in Java. For example, if the requirement is to remove “b” from “bring”, the output should be “ring”.

Download Required JAR Files:

We will use StringUtils class in Apache Common Components library. So, have a copy of commons-lang3-3.1.jar.

Java Program Example:

The Java code example for this tutorial is provided below:

import org.apache.commons.lang3.StringUtils; // to trim the string
public class removeSpecificCharacter {    
    public static void main (String[] args) {
    String myInput="bring";
    String removeCharacter="b";                     
    //This method trims the string
    System.out.println("Input: " + myInput);
    System.out.println("Trimmed : " + StringUtils.remove(myInput,removeCharacter));                 
    }
}

Example Output:


C:\Java\commons-lang3-3.1>javac -classpath .;commons-lang3-3.1.jar removeSpecificCharacter.java
C:\Java\commons-lang3-3.1>java -classpath .;commons-lang3-3.1.jar removeSpecificCharacter
Input: bring
Trimmed : ring

How to Trim a String in Java?

Requirement:

Trim a string in Java and remove all spaces ( before and after the word). For example “    ght   ” should be trimmed to “ght”.

Download JAR files:

Download commons-lang3-3.1.jar file from Apache website for this example to work.

Java Program Code:

The Complete Java code to Trim a string is provided below:

import org.apache.commons.lang3.StringUtils; // to trim the string
public class trimString {    
    public static void main (String[] args) {
                        String myInput="   ght  ";
                        String newRepeatedString=StringUtils.trimToEmpty(myInput);
                        //This method trims the string
                        System.out.println("Input: " + myInput);
                        System.out.println("Trimmed : " + newRepeatedString);                   
    }
}

Example Output:


C:\Java\commons-lang3-3.1>javac -classpath .;commons-lang3-3.1.jar trimString.java
C:\Java\commons-lang3-3.1>java -classpath .;commons-lang3-3.1.jar trimString
Input:    ght
Trimmed : ght

How to easily reverse a string in Java?

Requirement:

Reverse a string in Java in One line. For example, “train” produces “niart”.

JAR Files:

Download commons-lang3-3.1.jar JAR file for the example to work.

Java Code:

The Java code program to reverse a string is provided below:
import org.apache.commons.lang3.StringUtils; // to repeat the string
public class reverseString {    
    public static void main (String[] args) {
                        String myInput="train";
                        String newRepeatedString=StringUtils.reverse(myInput);
                        //This method reverses the string
                        System.out.println("Input String: " + myInput);
                        System.out.println("Reversed : " + newRepeatedString);                  
    }
}



Example Output:


C:\Java\commons-lang3-3.1>javac -classpath .;commons-lang3-3.1.jar reverseString
.java
C:\Java\commons-lang3-3.1>java -classpath .;commons-lang3-3.1.jar reverseString


Input String: train
Reversed : niart

How to repeat string with separator in Java?

Requirement:

Repeat string with a separator in Java. For example,
“Java” when repeated three times with a hyphen to produce “Java-Java-Java”

JAR Files Download:

Download a copy of commons-lang3-3.1.jar and keep it in your class path.

Java Code Example:

The program that you can use to repeat strings in Java with a separator is provided below:
import org.apache.commons.lang3.StringUtils; // to repeat the string
public class repeatStringSeparator {    
    public static void main (String[] args) {
        String myInput="Java";
        String separator="-";
        int number_of_times=3;
        String newRepeatedString=StringUtils.repeat(myInput,separator,number_of_times); 
        //we repeated the string 3 times
        System.out.println("Input String: " + myInput);
        System.out.println("Repeated String: " + newRepeatedString);                    
    }
}

Example Output:

C:\Java\commons-lang3-3.1>javac -classpath .;commons-lang3-3.1.jar repeatStringS
eparator.java

C:\Java\commons-lang3-3.1>java -classpath .;commons-lang3-3.1.jar repeatStringSe
parator
Input String: Java
Repeated String: Java-Java-Java

How to repeat a string multiple times in Java?

In this post, we will describe how to repeat a string multiple times in Java. To do this, we will use Apache Commons library.

Requirement:

I have the following word.."Six". ( without quotes). I want to construct, SixSixSixSixSix..(i.e. the word repeated 5 times). How to do this in Java?

JAR Files:

Let us do less coding to accomplish this. So, download commons-lang3-3.1.jar and make sure you have it in your classpath.

Solution:

The Java code is given below:

import org.apache.commons.lang3.StringUtils; // to repeat the string
public class repeatString {    
    public static void main (String[] args) {
                        String myInput="Six";
                        String newRepeatedString=StringUtils.repeat(myInput,5);
 //we repeated the string 5 times
                        System.out.println("Input String: " + myInput);
                        System.out.println("Repeated String: " + newRepeatedString);                    
    }
}

Compile / Run the Code


Compiling and executing the code produces the output we are looking for:

C:\Java\commons-lang3-3.1>javac -classpath .;commons-lang3-3.1.jar repeatString.
java
C:\Java\commons-lang3-3.1>java -classpath .;commons-lang3-3.1.jar repeatString
Input String: Six
Repeated String: SixSixSixSixSix
C:\Java\commons-lang3-3.1>



See you in a different tutorial next time.