Is it worth checking if a string contains a substring before replacing it?

I saw this code today

if (translatedText.contains("â")) translatedText = translatedText.replace("â", "a");
if (translatedText.contains("ê")) translatedText = translatedText.replace("ê", "e");
...

There are 22 lines like this, and I was wondering what is the point of using "ifs" before replacing. The way I understand it works, we read the line twice in the line, and calling the replace method directly will have the same effect when nothing is replaced, and it will be faster if there is something to replace.

But this is just how I think it works. Can someone confirm or correct me?

And the second question. We do this for each vowel and for each symbol "á", "à", "â" and "ä". I bet there is a better way to do this in Java. Any suggestions?

Thank.

+4
4

"": JVM; : replace(), ; .

: . :

- , :

Map<String, String> replacements = new HashMap<>();

:

replacements.put("â", "a");
...

, , / replace().

, , -

replacements.put("[áàâä]", "a");

replaceAll() .

+2

, replace , , Oracle (Java 8):

public String replace(CharSequence target, CharSequence replacement) {
    return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
            this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}

... , - , , (), .

, 22 , , ([âê] ..), , , ( ):

// You can do this part once somewhere if you want to
Pattern regex = Pattern.compile("[âê]");
// Then:
StringBuffer resultString = new StringBuffer();
Matcher regexMatcher = regex.matcher(translatedText);
while (regexMatcher.find()) {
    String match = regexMatch.group();
    String replacement;
    switch (match) {
        // ...various cases setting `replacement`
    }
    regexMatcher.appendReplacement(resultString, replacement);
}
regexMatcher.appendTail(resultString);
translatedText = resultString.toString();

- ( ):

// You can do this part once somewhere if you want to
Pattern regex = Pattern.compile("[âê]");
// Then:
StringBuffer resultString = null;
Matcher regexMatcher = regex.matcher(translatedText);
while (regexMatcher.find()) {
    if (resultString == null) {
        resultString = new StringBuffer(translatedText.length() + 100);
    }
    String match = regexMatch.group();
    String replacement;
    switch (match) {
        // ...various cases setting `replacement`
    }
    regexMatcher.appendReplacement(resultString, replacement);
}
if (resultString != null) {
    regexMatcher.appendTail(resultString);
    translatedText = resultString.toString();
}
+7

String s="sasaáàdaâadsasä";
System.out.println(s.replaceAll("[áàâä]", "a"));

:

sasaaadaaadsasa

[] ,

, if

String s="sasaáàdaâadsêêêasä";
String str=s.replaceAll("[áàâä]", "a").replaceAll("[ê]", "e");
System.out.println(str);

:

sasaaadaaadseeeasa
+2

if , replace(char, char):

translatedText = translatedText.replace('â', 'a');
translatedText = translatedText.replace('ê', 'e');

, , , Java 8 String, .

, , . .

+2

Source: https://habr.com/ru/post/1664159/


All Articles