[Java] How to erase a specific character from a character string

Introduction

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.

Implementation example

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.

Summary

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.

References

JavaDoc for replace method

Recommended Posts

[Java] How to erase a specific character from a character string
[Java] How to convert a character string from String type to byte type
[Java] How to cut out a character string character by character
[Java] How to use substring to cut out a character string
[Android] How to convert a character string to resourceId
[Java] How to use substring to cut out a part of a character string
[Java] How to get to the front of a specific string using the String class
How to jump from Eclipse Java to a SQL file
Ruby Regular Expression Extracts from a specific string to a string
How to make a Java container
[Java] How to create a folder
How to make a Java array
[Java] Divide a character string by a specified character
How to make a Discord bot (Java)
How to output Java string to console screen
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
How to print a Java Word document
[Java] How to convert from String to Path type and get the path
How to check for the contents of a java fixed-length string
Type conversion from java BigDecimal type to String type
[java] Summary of how to handle character strings
Connect to Aurora (MySQL) from a Java application
How to get a heapdump from a Docker container
How to display a web page in Java
To become a VB.net programmer from a Java shop
Code to escape a JSON string in Java
How to get Class from Element in Java
[Java] How to switch from open jdk to oracle jdk
[Ruby] How to generate a random alphabet string
How to write Java String # getBytes in Kotlin?
Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
[Java] How to convert one element of a String type array to an Int type
[Spring Boot] How to get properties dynamically from a string contained in a URL
[Java] How to easily get the longest character string of ArrayList using stream
How to create a Java environment in just 3 seconds
How to write Scala from the perspective of Java
How to deploy to Heroku from a local docker image
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name
java: How to write a generic type list [Note]
[Java] How to play rock-paper-scissors (equivalent to paiza rank A)
[Java] How to extract the file name from the path
Search for character string B from character string A (duplicate count)
How to make JavaScript work on a specific page
How to create a data URI (base64) in Java
[Java] How to get a request by HTTP communication
How to change from Oracle Java 8 to Adopt Open JDK 9
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
[Rails] How to load JavaScript in a specific view
[Java] How to start a new line with StringBuilder
[Java] Incompatible types error occurred when assigning a character string to a char type variable
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to use java Optional
New features from Java7 to Java8
How to minimize Java images