As the title suggests, I had to remove certain characters from a string in my work.
I wrote an article about how that process was done, albeit briefly.
Now suppose you want to remove {} from the string enclosed in {}.
At this time, {} can be deleted by using the replace method of the String class as shown below.
sample.java
public class Main {
public static void main(String[] args) throws Exception {
String test = "{test}";
String result = test.replace("{", "").replace("}", "");
System.out.println(result); //test is output
System.out.println(result.length()); //4 is output
}
}
By specifying the character you want to erase in the first argument of replace and passing an empty string in the second argument, you can erase a specific character.
To delete a specific character from a character string, use the replace method of the String class, and pass the character string you want to delete as the first argument and an empty string as the second argument.
If there is a smarter way, I would appreciate it if you could teach me.
I hope this article helps someone. Until the end Thank you for reading.
Recommended Posts