What is the optimized way of finding and replacing specific set of strings in a lengthy string?

krishna

I need to obfuscate values of parameters matching

password, tokenID

Sample query string:

visitorNo=89&password=demo&tokenID=yxr56

Should be obfuscated to:

visitorNo=89&password=$$&tokenID=$$

What I did:

String[] parameters = queryString.split("&");
StringBuffer qS = new StringBuffer();
for(String param : parameters) {
    String[] keyValue = param.split("=");
    qS.append(keyValue[0]);
    qS.append("=");
    for(String paramToObfuscate : paramsToObfuscate) {
        if(paramToObfuscate.equals(keyValue[0])) {
            qS.append("$$");
        }
        else {
            qS.append(keyValue[1]);
        }
    }
    qS.append("&");
}

String queryStr = qS.toString().substring(0, qS.length-1);
maaartinus

For only two parameters, there's no faster way. If there were tens of them, you could use

Set<String> paramsToObfuscate = new HashSet<String>();

and Set.contains, which is surely faster than tens of tests.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Optimized way of finding similar strings

From Dev

What is the optimized way of finding count? LINQ or foreach?

From Dev

Finding a specific string in array of strings

From Dev

Optimized way for finding the number which satisfies specific conditions

From Dev

Finding and replacing strings

From Dev

Finding and replacing strings

From Dev

What is the fastest way to compare set of strings to one string?

From Dev

What is the fastest way to compare set of strings to one string?

From Dev

Finding a string between strings in efficient way

From Dev

What is optimized way of forloop

From Java

Most optimized way of concatenation in strings

From Dev

Is there a way to join strings, each with a specific surrounding string?

From Dev

What is the best way to store a set of strings

From Dev

Pythonic way to match a string if contained in a set of strings

From Dev

Finding and Replacing multiple strings in an XML file

From Dev

Finding and Replacing multiple strings in an XML file

From Dev

Replacing specific occurrences of strings in Python

From Dev

Replacing specific occurrences of strings in Python

From Dev

Replacing a specific character in a string

From Dev

Finding file and replacing word with some specific word

From Dev

Finding, and replacing, a very specific pattern with regexprep

From Dev

What is the proper way to remove specific string in arraylist?

From Dev

Looking for an optimized way of replacing list patterns in long documents

From Dev

Replacing string between Specific String

From Dev

Replacing string between Specific String

From Dev

Finding suffixes in a set of strings, Python

From Dev

Optimized way for Finding prime numbers upto given value n

From Dev

Optimized way for Finding prime numbers upto given value n

From Dev

Finding and replacing the parentheses in a string in Elm 0.16

Related Related

  1. 1

    Optimized way of finding similar strings

  2. 2

    What is the optimized way of finding count? LINQ or foreach?

  3. 3

    Finding a specific string in array of strings

  4. 4

    Optimized way for finding the number which satisfies specific conditions

  5. 5

    Finding and replacing strings

  6. 6

    Finding and replacing strings

  7. 7

    What is the fastest way to compare set of strings to one string?

  8. 8

    What is the fastest way to compare set of strings to one string?

  9. 9

    Finding a string between strings in efficient way

  10. 10

    What is optimized way of forloop

  11. 11

    Most optimized way of concatenation in strings

  12. 12

    Is there a way to join strings, each with a specific surrounding string?

  13. 13

    What is the best way to store a set of strings

  14. 14

    Pythonic way to match a string if contained in a set of strings

  15. 15

    Finding and Replacing multiple strings in an XML file

  16. 16

    Finding and Replacing multiple strings in an XML file

  17. 17

    Replacing specific occurrences of strings in Python

  18. 18

    Replacing specific occurrences of strings in Python

  19. 19

    Replacing a specific character in a string

  20. 20

    Finding file and replacing word with some specific word

  21. 21

    Finding, and replacing, a very specific pattern with regexprep

  22. 22

    What is the proper way to remove specific string in arraylist?

  23. 23

    Looking for an optimized way of replacing list patterns in long documents

  24. 24

    Replacing string between Specific String

  25. 25

    Replacing string between Specific String

  26. 26

    Finding suffixes in a set of strings, Python

  27. 27

    Optimized way for Finding prime numbers upto given value n

  28. 28

    Optimized way for Finding prime numbers upto given value n

  29. 29

    Finding and replacing the parentheses in a string in Elm 0.16

HotTag

Archive