Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing bad characters of a String with bad characters

I just wondered what's the easiest way to replace a string characters that must be replaced subsequently.

For example:

var str = "[Hello World]";
//enclose all occurences of [ and ] with brackets[] 
str = str.Replace("[","[[]").Replace("]","[]]");
  • The desired result: [[]Hello World[]]
  • The actual result: [[[]]Hello World[]]

The reason is obviously the second replace on the already modified string.

So how to replace all occurences of "bad" characters with characters that contain "bad" characters?


A quick measurement of all approaches revealed that the StringBuilder is the most efficient way.

190kb file (all in milliseconds)

  regexTime           40.5065  
  replaceTime         20.8891  
  stringBuilderTime    6.9776

7MB file

  regexTime           1209.3529           
  replaceTime          403.3985   
  stringBuilderTime    175.2583

By the way, the direct StringBuilder approach from John was twice as fast as the Aggregate approach from Sehe.

I've made an extension out of it:

public static String EncloseChars(this string input, char[] charsToEnclose, String leftSide, String rightSide) {
    if (charsToEnclose == null || leftSide == null || rightSide == null)
        throw new ArgumentException("Invalid arguments for EncloseChars", charsToEnclose == null ? "charsToEnclose" : leftSide == null ? "leftSide" : "rightSide");
    Array.Sort(charsToEnclose);
    StringBuilder sb = new StringBuilder();
    foreach (char c in input) {
        if (Array.BinarySearch(charsToEnclose, c) > -1)
            sb.Append(leftSide).Append(c).Append(rightSide);
        else 
            sb.Append(c);
    }
    return sb.ToString();
}

"[Hello World]".EncloseChars(new char[]{'[', ']'},"[","]");
like image 973
Tim Schmelter Avatar asked Feb 13 '12 16:02

Tim Schmelter


People also ask

How do I remove bad characters from a string in Python?

Removing symbol from string using replace() One can use str. replace() inside a loop to check for a bad_char and then replace it with the empty string hence removing it.

How do you replace all characters in a string?

To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag.

Can you change a char in a string?

String are immutable in Java. You can't change them. You need to create a new string with the character replaced.


2 Answers

Here's a very uncool way to do it. But it has the advantage of being pretty close to foolproof, I think, and not using regex (in case you'd rather not use regex).

StringBuilder sb = new StringBuilder();
foreach (char c in str.ToCharArray()) {
    if (c == '[' || c == ']') {
        sb.Append('[' + c + ']');
    }
    else {
        sb.Append(c);
    }
}
string result = sb.ToString();
like image 86
John M Gant Avatar answered Sep 21 '22 16:09

John M Gant


What about:

str = str.Replace("[", "$1[$2")
         .Replace("]", "$1]$2")
         .Replace("$1", "[")
         .Replace("$2", "]");
like image 44
mfeineis Avatar answered Sep 21 '22 16:09

mfeineis