Home> Java> javaTutorial> body text

How to replace all matching content in a string using replaceAll() method of String class

PHPz
Release: 2023-07-26 08:18:07
Original
3801 people have browsed it

How to use the replaceAll() method of the String class to replace all matching content in the string

Introduction:
In the process of string processing, sometimes we need to replace all matching content in the string The content is replaced with the specified content. Java provides the replaceAll() method of the String class to implement this function. This article will introduce the use of the replaceAll() method and provide some practical code examples.

1. Basic usage of replaceAll() method
The replaceAll() method is a common method of the String class. Its function is to replace all matching content in the string with the specified content. This method accepts two parameters, the first parameter is the regular expression used for matching, and the second parameter is the replacement content.

For example, we have a string str and need to replace all the numbers in the string with "#". You can use the following code:

String str = "a1b2c3"; String replacedStr = str.replaceAll("\d", "#"); System.out.println(replacedStr);
Copy after login

The output result is: "a#b# c#"

In this example, we use the regular expression "\d" to represent numbers. In regular expressions, "d" represents a numeric character. Since the backslash in the regular expression is an escape character, we need to use "\d" to represent a backslash and a "d" to match a numeric character. After replacing the numeric characters with "#", the result is "a#b#c#".

It should be noted that the replaceAll() method will return a new string, and the original string will not be modified.

2. Use the replaceAll() method to replace all matching content in the string
In addition to replacing numbers, the replaceAll() method can also replace other matching content in the string, such as replacing specific letters, replacing Strings and so on. Next, we demonstrate these usages with actual code examples.

  1. Replace specific letters

    String str = "Hello World"; String replacedStr = str.replaceAll("o", "*"); System.out.println(replacedStr);
    Copy after login

    The output result is: "HellWrld"

In this example, we replace all letters "o" in the string with "". The result is "HellW*rld".

  1. Replace the string

    String str = "Java is awesome"; String replacedStr = str.replaceAll("awesome", "amazing"); System.out.println(replacedStr);
    Copy after login

    The output result is: "Java is amazing"

In this example, we replace the characters Replace "awesome" in the string with "amazing". The result is "Java is amazing".

  1. Use regular expression to replace matching content

    String str = "Hello, 123.45!"; String replacedStr = str.replaceAll("[^\d.]", ""); System.out.println(replacedStr);
    Copy after login

    The output result is: "123.45"

    This example shows how Use regular expressions to replace matching content. In this example, we use "1" to represent non-digit and non-decimal characters. After replacing non-digit and non-decimal point characters with null characters (""), the result is "123.45".

    It should be noted that the syntax of regular expressions is very flexible and can be adjusted according to actual needs.

    Conclusion:
    This article introduces how to use the replaceAll() method of the String class to replace all matching content in a string. Through demonstrations of the basic usage of the replaceAll() method and actual code examples, we learned how to use different regular expressions to replace content in a string. After mastering the skills of using the replaceAll() method, you can complete the replacement operation more conveniently during string processing. I hope this article will be helpful to your study.


    1. d.

    The above is the detailed content of How to replace all matching content in a string using replaceAll() method of String class. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!