. Advertisement .
..3..
. Advertisement .
..4..
ITTutoria keeps receiving the same question, “How can I remove character from string in Java?” So today, we wrote this article to shed some light on your burning inquiries. This article will introduce three simple methods, easy for even a novice to perform!
How to Remove Character from String in Java
Method 1: Use the String.Substring
The notable concept here is to employ sub(string) from the String categories to extract the last and the first string character. The substring(int/beginIndex, int/endIndex) operations receive two parameters. The first one is a starting index, while the second serves as an ending index.
The first string letter exists at the “zero” index”, while the last thrives at the “-1” string length.
Now, all you need to do is to remove the substring (remove the first two characters and the last three characters) via str.substring(2, str.length() – 3). Once done, print your modified string.
Below is an example:
// Create a Java program to remove the first two characters and the last three characters of a string
class GFG {
// Function to remove the first two characters and the last three characters of a string
public static String
remove_character(String example_str)
{
// Removing the first two characters and the last three characters of a string using substring() method
example_str = example_str.substring(2, example_str.length() - 3);
// Return the modified string
return example_str;
}
// Driver Code
public static void main(String args[])
{
// Given String example_str
String example_str = "!!How to Remove Character from String in Java?#!";
// Print the modified string
System.out.print(
remove_character(example_str));
}
}
Output:
How to Remove Character from String in Java
Sometimes, the “java.time.Instant” error might pop up (often when you employ Lombok plugins). To fix this issue, please refer to this guide.
Method 2: Use Replace()
Another tactic is to employ Replace() to extract one character from the string before substituting it with empty strings.
The basic syntax is:
example_string.replace(target, replacement);
- example_string – example_string is a string where one character needs removal.
- target – it represents the removed character
- replacement – it implies the new character that you use to substitute the old one.
Let’s take “example_string = Remove Character from String in Java!” as an example. Here, you will want to remove the ‘!’ character. We will show you how to extract this letter using Replace().
Code:
class GFG {
public static void main(String args[]) {
String example_string = "Remove Character from String in Java!";
System.out.println("Length of the original string: " + example_string.length());
example_string = example_string.replace("!","");
System.out.println(example_string);
System.out.println("Length of the string after removing: " + example_string.length());
Output:
Length of the original string: 37
Remove Character from String in Java
Length of the string after removing: 36
Method 3: Use “Guava”
Do you prefer Guava libraries instead? Then you may employ its CharMatcher category by extracting some string characters. You need to obtain one char matcher that suits all the specified characters. Next, remove those matching letters from the specified letter collection via RemoveFrom().
Please look at the illustration below:
import com.google.common.base.CharMatcher;
public class main
{
public static void main(String[] args)
{
String exp_string1 = "Welcome to, Ittutoria_!!";
String character_Remove = ",_!";
exp_string1 = CharMatcher.anyOf(character_Remove).removeFrom(exp_string1);
System.out.println(exp_string1);
}
}
Output:
Welcome to Ittutoria
Conclusion
This ITtutoria article has answered your question, “How can I remove character from string in Java?” We hope our tips will prove practical for your programs. Feel free to contact us for more support and advice!
Leave a comment