How to Retrieve the Number Replaced by String.replace()

Asked 2 years ago, Updated 2 years ago, 33 views

I'm looking on the Internet and Java Doc to find out how to get the number replaced by Java's String.replace().
Unknown.

If you run String test; with long text as test.replace("Ai", "ABC";,
I'd like to get how many have been replaced, but could you get it with some member variable/function of String?

If it is not possible, is it the only way to count numbers with indexof("Ai", nowIndex+1) in the for statement before replacing?

java

2022-09-30 20:23

4 Answers

Since the number of substitutions cannot be retrieved by String member variables/functions, you have no choice but to count the number before substituting using other answers, such as methods or for statements, regular expression.

If you are allowed to use an external library, it would be easier to use Apache commons-lang's StringUtils.countMatches method.

リンクThe link is a sample code for each stack overflow English version of the answer.


2022-09-30 20:23

Before you run (replace("Ai", "ABC"), I think you have no choice but to find out how many "Ai" are included in the test.
Rather than checking indexof in order, it is faster to find out how many characters disappear after deleting the string ("Ai") and divide it by the number of characters (like the code below).

String test_work;
origin_length;
int number_of_aiu;

Make a copy of test_work=test.toString(); // test.
origin_length = length(test); // The original number of characters for the test.
Create a string with test_work.replace( あいAi ", ");// "Aiう deleted.
number_of_aiu=(orig_length-length(test_work)/length("Ai"); // The number of "Ai" included in the test.


2022-09-30 20:23

Is it possible to get it with some String member variable/function?

I don't think so.

If it is not possible, is it the only way to count numbers with indexof("Ai", nowIndex+1) in the for statement before replacing it?

String#replace(...) is implemented as follows:

public String replace (CharSequence target, CharSequence replacement) {
  return Pattern.compile(target.toString(), Pattern.LITERAL)
                .matcher(this)
                .replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
  • The contents of the new string can be calculated from match results as needed. You can use the appendReplacement and appendTail methods at the same time to collect the results and store them in an existing string buffer.

The contents of the new string can be calculated from match results as needed. You can use the appendReplacement and appendTail methods at the same time to collect the results and store them in an existing string buffer.

It would be more efficient to count the number of replacements by referring to such things as

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexDemo{
  private static final String REGEX="dog";
  private static final String INPUT="dog dog doggie dogg";
  private static final String REPLACE = Matcher.quoteReplacement("ABC");
  public static void main(String[]args) {
    Pattern p = Pattern.compile(REGEX, Pattern.LITERAL);
    Matcher m=p.matcher (INPUT);
    int count = 0;
    StringBuffersb = new StringBuffer();
    while(m.find()){
      count++;
      m.appendReplacement(sb,REPLACE);
    }
    m.appendTail(sb);
    System.out.println("count:"+count);
    System.out.println(sb.toString());
    // System.out.println(m.replaceAll(REPLACE));
  }
}


2022-09-30 20:23

using split Number of replacements: test.split("Ai", -1).length-1
It may be easy to say that

sThe first argument of split is a regular expression string, so you need to escape the characters that correspond to the regular expression.


2022-09-30 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.