Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# remove parenthesis from string

This seems to be a common question for C# users and after research and multiple attempts I cant for the life of me remove a pair of parenthesis from a string. The string I am having a problem with is Service (additional).

After doing my research I understand parenthesis are treated differently in Regex.Replace. With my research also came multiple answers that I attempted but nothing seems to have worked. Here are some ways that I have tried to remove these parenthesis.

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 these worked, after stepping through the code I just see the the space between the 'e' and '(' removed. Am I missing something?

In case anyone wanted to see the function that is being used here it 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;
    }
like image 1000
user2405778 Avatar asked Feb 14 '14 18:02

user2405778


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


2 Answers

A Regex is overkill here as this can be done with a simple Replace call:

string val = intVal.Replace("(", "").Replace(")", "");
like image 62
JaredPar Avatar answered Oct 03 '22 12:10

JaredPar


After your call to Regex.Replace(...) you're actually using string.Replace(...). This makes your call to .Replace(@"[^a-zA-Z]", "") useless.

Simplify it instead to:

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

This should remove all spaces and special characters which is what it looks like your code is trying to do. This includes parentheses.

like image 45
McAden Avatar answered Oct 03 '22 10:10

McAden