Replacing bad characters of a String with bad characters

前端 未结 6 1410
走了就别回头了
走了就别回头了 2020-12-11 17:12

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

For example:

var str = \"[Hello World]\";         


        
6条回答
  •  没有蜡笔的小新
    2020-12-11 17:26

        StringBuilder result = new StringBuilder();
    
        foreach (Char singleCharacter in str)
        {
            result.Append(singleCharacter.Equals('[') ? "[[]" : singleCharacter.Equals(']') ? "[]]" : singleCharacter.ToString());
        }
    
        str = result.ToString();
    

提交回复
热议问题