C # remove brackets from string

This is apparently a common question for C # users, and after researching and several attempts that I cannot use for life, I remove a couple of brackets from the string. I have a problem with Service (additional).

After doing my research, I understand that parentheses are handled differently in Regex.Replace. My research also came up with several answers that I tried, but nothing seemed to work. Here are some ways I tried to remove these brackets.

cleanValue = Regex.Replace(intVal, " ", "").Replace("(", "").Replace(")", "").Replace(",", "").Replace("/", "").Replace("-", "");

cleanValue = Regex.Replace(intVal, " ", "").Replace(@"\(", "").Replace(@"\)", "").Replace(",", "").Replace("/", "").Replace("-", "");

cleanValue = Regex.Replace(intVal, " ", "").Replace("[()]", "").Replace(",", "").Replace("/", "").Replace("-", "");

cleanValue = Regex.Replace(intVal, " ", "").Replace(@"[^a-zA-Z]", "").Replace(",", "").Replace("/", "").Replace("-", "");

None of them worked, after I went through the code, I just see the space between "e" and "(" deleted ". Did I miss something?

In case someone wants to see functionwhich one is used here, this is:

    public static string CleanExtra(string intVal)
    {
        string cleanValue;
        if (intVal == null)
        {
            throw new System.ArgumentException("Value cannot be null", "original");
        }
        else
        {
            //cleanValue = Regex.Replace(intVal, " ", "").Replace("(", "").Replace(")", "").Replace(",", "").Replace("/", "").Replace("-", "");
            //cleanValue = Regex.Replace(intVal, " ", "").Replace(@"\(", "").Replace(@"\)", "").Replace(",", "").Replace("/", "").Replace("-", "");
            //cleanValue = Regex.Replace(intVal, " ", "").Replace("[()]", "").Replace(",", "").Replace("/", "").Replace("-", "");
            cleanValue = Regex.Replace(intVal, " ", "").Replace(@"[^a-zA-Z]", "").Replace(",", "").Replace("/", "").Replace("-", "");
        }

        return cleanValue;
    }
+4
3

Regex.Replace(...) string.Replace(...). .Replace(@"[^a-zA-Z]", "") .

:

cleanValue = Regex.Replace(intVal, @"[^a-zA-Z]", "");

, , . .

+5

Regex , Replace:

string val = intVal.Replace("(", "").Replace(")", "");
+14

This is because every second Replaceis a call to stringand therefore does not replace it with a regular expression.

+3
source

Source: https://habr.com/ru/post/1527007/


All Articles