1

I've got a string like so

Jamie(123)

And I'm trying to just show Jamie without the brackets etc

All the names are different lengths so I was wondering if there was a simple way of replacing everything from the first bracket onwards?

Some others are displayed like this

Tom(Test(123))
Jack   ((4u72))

I've got a simple replace of the bracket at the moment like this

mystring.Replace("(", "").Replace(")","")

Any help would be appreciated

Thanks

0

4 Answers 4

1

VB.NET

mystring.Substring(0, mystring.IndexOf("("C)).Trim()

C#

mystring.Substring(0, mystring.IndexOf('(')).Trim();
Sign up to request clarification or add additional context in comments.

Comments

0

One logic; get the index of the ( and you can trim the later part from that position.

public static string Remove(string value)
            {
                int pos = value.IndexOf("(");
                if (pos >= 0)
                {
                    return value.Remove(pos, remove.Length);
                }
                return value;
            }

Comments

0

aneal's will work. The alternative I generally use because it's a bit more flexible is .substring.

string newstring = oldstring.substring(0,oldstring.indexof("("));

If you aren't sure that oldstring will have a "(" you will have to do the test first just as aneal shows in their answer.

Comments

0

String.Remove(Int32) will do what you need:

Deletes all the characters from this string beginning at a 
specified position and continuing through the last position.

You will also have to .Trim() as well given the data with padding:

mystring = mystring.Remove(mystring.IndexOf("("C))).Trim()

3 Comments

MSDN says Remove takes an int, no? See msdn.microsoft.com/en-us/library/9ad138yc.aspx
trim() - Removes all leading and trailing white-space characters from the current String object. See msdn.microsoft.com/en-us/library/t97s7bs3.aspx
Yes, .Remove returns a string object, then it gets trimmed and assinged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.