0

I am wondering what would be the best way to specify an array of characters like,

{
}
[
]

and then check a string for these and if they are there, to completely remove them.

        if (compiler.Parser.GetErrors().Count == 0)
        {
            AstNode root = compiler.Parse(phrase.ToLower());
            if (compiler.Parser.GetErrors().Count == 0)
            {
                try
                {
                    fTextSearch = SearchGrammar.ConvertQuery(root, SearchGrammar.TermType.Inflectional);
                }
                catch
                {
                    fTextSearch = phrase;
                }
            }
            else
            {
                fTextSearch = phrase;
            }
        }
        else
        {
            fTextSearch = phrase;
        }

        string[] brackets = brackets = new string[]
        {
            "{",
            "}",
            "[",
            "]"
        };

        string[] errorChars = errorChars = new string[]
        {
            "'",
            "&"
        };

        StringBuilder sb = new StringBuilder();

        string[] splitString = fTextSearch.Split(errorChars, StringSplitOptions.None);

        int numNewCharactersAdded = 0;
        foreach (string itm in splitString)
        {
            sb.Append(itm); //append string
            if (fTextSearch.Length > (sb.Length - numNewCharactersAdded))
            {
                sb.Append(fTextSearch[sb.Length - numNewCharactersAdded]); //append splitting character
                sb.Append(fTextSearch[sb.Length - numNewCharactersAdded - 1]); //append it again
                numNewCharactersAdded++;
            }
        }

        string newString = sb.ToString();
Hello World
  • 1,379
  • 4
  • 20
  • 41

8 Answers8

11

A regular expression can do this far more easily:

var result = Regex.Replace(input, @"[[\]()]", "");

Using a character set ([...]) to match anyone of the characters in it and replace with nothing. Regex.Replace will replace all matches.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • don't you need to escape the 2nd open square bracket? (as well as the rounded brackets) `@"[\[\]\(\)]"`? – James Jul 25 '12 at 15:30
  • 2
    @James No: the set of metacharacters inside a character class is much smaller than outside (eg. `[.]` will onoly match a period). – Richard Jul 25 '12 at 15:31
5

Another concise way is using Enumerable.Except to get the set difference of the Chars(assuming brackets are chars):

String newString = new String(oldString.Except(brackets).ToArray());
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • BROKEN, BE WARNED: like stated in the link -> Produces the SET difference of two sequences. So, if oldString contains multiple of the same char only ONE gets in the result ! -> sooome.one will produce some.ne ? See comments on https://stackoverflow.com/a/2231142/1498669 – Bernhard Jan 15 '20 at 08:57
2
string str = "faslkjnro(fjrmn){ferqwe}{{";
char[] separators = new []{'[', ']','{','}' };
var sb = new StringBuilder();
foreach (var c in str)
{
    if (!separators.Contains(c))
    {
        sb.Append(c);
    }
}

return sb.ToString();
oleksii
  • 35,458
  • 16
  • 93
  • 163
0

How about this:

string myString = "a12{drr[ferr]vgb}rtg";
myString = myString.Replace("[", "").Replace("{", "").Replace("]", "").Replace("}", "");

You end up with:

a12drrferrvgbrtg

JMK
  • 27,273
  • 52
  • 163
  • 280
  • 1
    I wonder if `StringBuilder.Replace` might be better if there are a significant number of to be removed characters? It would certainly avoid the intermediate values in the method chain. Perhaps only an issue if this is in a loop of the input is long. – Richard Jul 25 '12 at 15:33
  • Fair point, your answer is better anyway lol, I haven't taken the time to learn Regex yet, should really do that! – JMK Jul 25 '12 at 15:36
0

I don't know if I understand your problem, but you can solve your problem with this:

string toRemove = "{}[]";
string result = your_string_to_be_searched;
foreach(char c in toRemove)
    result = result.Replace(c.ToString(), "");

or with an extension method

static class Extensions
{
    public static string RemoveAll(this string src, string chars)
    {
        foreach(char c in chars)
            src= src.Replace(c.ToString(), "");
        return src;
    }
}

With this you can use string result = your_string_to_be_searched.RemoveAll("{}[]");

Marco
  • 56,740
  • 14
  • 129
  • 152
  • There is no `String.Replace(char, string)` overload: are you missing a `ToString`? – Richard Jul 25 '12 at 15:29
  • @Richard: yes, I had to edit my answer adding also another part. But your answer is the one :) Thanks – Marco Jul 25 '12 at 15:33
0
string charsToRemove = @"[]{}";
string pattern = string.Format("[{0}]", Regex.Escape(charsToRemove));
var result = Regex.Replace(input, pattern, "");

The primary advantage of this over some of the other similar answers is that you aren't bothered with determining which characters need to be escaped in RegEx; you can let the library take care of that for you.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Given the set of metachars inside a character class is different to outside (eg. `[.]` only matches a period character) I'm not sure `Regex.Escape` will do the right thing in all cases. – Richard Jul 25 '12 at 15:35
  • @Richard It may very well be escaping characters that it doesn't need to, but escaping characters that you don't need to doesn't cause it to not work properly. The fact that the set of characters needing to be escaped varies makes it all the more confusing, and thus all the more useful to not need to bother thinking about it yourself. If you can find an example set of inputs that doesn't work using this code then I'd life to know about it. – Servy Jul 25 '12 at 15:40
  • More a case of accidental collisions when converting "list of invalid chars" into string before escaping. (Consider `\` and `b` being in that list.) This is a case of while I cannot immediately think of an issue case, I cannot be sure there is no such combination (and thus why I didn't generalise by answer). – Richard Jul 25 '12 at 15:44
0

You can do this in a pretty compact fashion like this:

string s = "ab{c[d]}";
char[] ca = new char[] {'{', '}', '[', ']'};
Array.ForEach(ca, e => s = s.Replace(e.ToString(), ""));

Or this:

StringBuilder s = new StringBuilder("ab{c[d]}");
char[] ca = new char[] {'{', '}', '[', ']'};
Array.ForEach(ca, e => s.Replace(e.ToString(), ""));
JamieSee
  • 12,696
  • 2
  • 31
  • 47
0

Taken from this answer: https://stackoverflow.com/a/12800424/1498669

Just use .Split() with the char[] of your desired removeables and recapture it with .Join() or .Concat()

char[] delChars = "[]{}<>()".ToCharArray();
string input = "some (crazy) string with brac[et]s in{si}de";

string output = string.Join(string.Empty, input.Split(delChars));
//or
string output = string.Concat(input.Split(delChars));

References:

https://learn.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split https://learn.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings#code-try-4

Bernhard
  • 2,541
  • 1
  • 26
  • 24