Eric G
Eric G

Reputation: 926

Checking if a string needs to call String.replaceAll() vs calling it

Sorry if the title is misleading/bad, wasnt sure how to word this question.

I have multiple lists of strings, and most of them will contain parameters that I need to replace a string with. Some of these strings (in the list) will contain multiple parameters to replace.

Ex

Comp Status `%compname`
Comp SLA `%rackname` `%sgname` `%compname`

I will need to replace anything within the graves `%----` with a string, which depends on the position in a for loop.

My question is, should I check the string to see if it has one of these parameters i need to switch, or should i just call all the replaceAll() statements on every string.

1.

for(String s: rackStr){
    newString = new String[1];                
    s = s.replaceAll("`%rackname`", rName);
    s = s.replaceAll("`%compname`", compName);
    s = s.replaceAll("`%sysname`", sysName);
    newString[0] = s;
    System.out.println(newString[0]);
    vars.add(newString);
}

2.

for(String s: rackStr){
    newString = new String[1];       
    if(s.contains("`%rackname")){         
        s = s.replaceAll("`%rackname`", rName);
    }
    if(s.contains("`%compname")){         
    s = s.replaceAll("`%compname`", compName);
    }

    if(s.contains("`%sysname")){         
        s = s.replaceAll("`%sysname`", sysName);
    }

    newString[0] = s;
    System.out.println(newString[0]);
    vars.add(newString);
}

Note i have to make newString an array, and add it to an List vars so i can write the entire vars variable to a CSV file via OpenCSV writeAll();

So should I use 1 or 2? Or something different?

Upvotes: 1

Views: 96

Answers (3)

Mena
Mena

Reputation: 48404

First of all, you shouldn't invoke replaceAll if you're not using regular expressions to define your values to be replaced (in your case, they are clearly constant values so no need to abstract pattern definitions).

Use replace instead (it will replace all occurrences found).

It will internally invoke Matcher#replaceAll, but with constant values representing your fields to replace.

Invoking contains before invoking replace would not make a lot of sense, as you'd parse the String twice.

If the String doesn't contain the term to replace, no exception will be thrown: it just won't replace it.

You can also chain invocations for some eye candy:

s = s
    .replace("`%rackname`", rName)
    .replace("`%compname`", compName)
    .replace("`%sysname`", sysName);

Upvotes: 3

Will Hartung
Will Hartung

Reputation: 118611

Simple truth is that it depends.

The check itself has overhead, but the replace is not free.

If you had LOTS of different parameters, then at some point it would be cheaper to parse out exactly which parameters rather than blindly applying them all, since likely the majority wouldn't apply.

If your strings were particularly long, it would make sense also so as to not unnecessarily repeatedly parse the strings.

The most efficient technique is to do it in a stream, parsing the string just once, and applying the replacements as they show up.

But for 3 parameters over some short strings, simply brute force replacement while not not the most efficient in runtime, is far more efficient in development and maintenance. And the difference in runtime efficiencies is likely not enough to matter in the application.

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Prefer first way, because you need not to check in the first place weather it contains or not. And I would like to write the 3 lines in one line

s = s.replaceAll("`%rackname`", rName).replaceAll("`%compname`", compName).replaceAll("`%sysname`", sysName);

Upvotes: 1

Related Questions