Hi Everyone

I need some help. I would like to remove characters from a string that are contained in brackets. For example string (Hello)World, I want to remove the (Hello) part. Is there a way. I can't solve it with the replace or remove methods. I am new to C# and some help would be great.

Thank you

Johan

Recommended Answers

Perhaps have a look at(to use with Remove) the IndexOf('(') and LastIndexOf(')') methods.
Also the SubString method can be helpfull.
Look up the details here: http://msdn.microsoft.com/en-us/library/system.string_members(v=VS.90).aspx

Jump to Post

You could use the Regex class to do this. You would use a string such as \\(.\\) (with RegexOptions.SingleLine to have the dot (.) operator include every character). Then use the Regex.Replace() method.

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

Jump to Post

All 7 Replies

Perhaps have a look at(to use with Remove) the IndexOf('(') and LastIndexOf(')') methods.
Also the SubString method can be helpfull.
Look up the details here: http://msdn.microsoft.com/en-us/library/system.string_members(v=VS.90).aspx

Thanks I'll give it a try

Thanks @nmaillet too :)

I did something like this to solve my problem. Maybe it helps someone else.

private void btnRemove_Click(object sender, EventArgs e)
        {
            String strTest = "(Test)Word";

            int iS = strTest.IndexOf('(');

            int jS = strTest.LastIndexOf(')');

            String newOne = strTest.Remove(iS, jS + 1);

            MessageBox.Show(newOne);
}

The output is Word.

Thanks to everyone for helping me out.

Johan

What happens when you have "(Test)Word(Broken)"?

Hello

Didn't think about it. In my case there's only one bracketed word so that will do just fine :)

Johan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.