Metro Smurf
Metro Smurf

Reputation: 38335

Parsing a string in C#; is there a cleaner way?

C#, .NET 3.5

This just smells of ugly to me, but I can't think of another way.

Given a string with the format of "Joe Smith (jsmith)" (sans quotes), I'd like to parse out just the 'jsmith' string within the parenthesis. I've come up with this:

private static string DecipherUserName( string user )
{
    if( !user.Contains( "(" ) )
        return user;

    int start = user.IndexOf( "(" );

    return user.Substring( start ).Replace( "(", string.Empty ).Replace( ")", string.Empty );
}

Other than my (un)healthy aversion to RegEx, is there a simpler way to parse out the substring?

Edit: To clarify, the string to parse will always be of: "Joe Smith (jsmith)" (sans quotes).

Upvotes: 5

Views: 20548

Answers (6)

Matthew Sposato
Matthew Sposato

Reputation: 1635

If the user string is always in the form "Joe Smith (jsmith)", this should work.

private static string DecipherUserName(string user)
{
    string[] names = user.Split(new char[] {'(', ')'});
    return names.Length > 2 ? names[1] : user;
}

And if the user string is always "Joe Smith (jsmith)", this will always work.

private static string DecipherUserName(string user)
{
    return "jsmith";
}

The second entry for humor purposes only.

Upvotes: 5

Shea
Shea

Reputation: 11243

Regexes are so useful that you'll save yourself a ton of heartache biting the bullet and learning them. Not the whole shebang, just the basics.

One regex that'll work is "\w+\((.*)\)" - jsmith would be in Match.Groups[1].

One easy way to pick up regexes is to find a website that'll let you type in a regex and some text then spit out the matches...

Upvotes: 20

Daniel Brückner
Daniel Brückner

Reputation: 59645

Kind of a hack ... ^^

return user.Substring(user.IndexOf('(') + 1).TrimEnd(')');

If user contains no opening parenthesis, IndexOf() returns -1, we add one, get zero, and SubString() returns the whole string. TrimEnd() will have no effect unless the user's name ends with a closing parenthesis.

If user contains a opening parenthesis, IndexOf() returns its index, we skipp the opening parenthesis by adding one, and extract the rest of the string with Substring(). Finally we remove the closing parenthesis with TrimEnd().

Upvotes: 5

MartinStettner
MartinStettner

Reputation: 29164

I'd use

int start=user.IndexOf('(');
if (start != -1) {
  end = user.IndexOf(')', start);
  return user.Substring(start+1,end-start-1);
} else
  return user;

But this is just a cosmetic change: using characters in IndexOf is a little bit faster, and using the Substring method seems to express more exactly what should be done (and the method is more robust if you have multiple pairs of parentheses...)

That said, Daniel L's method (using String.Split) might be simpler (but doesn't deal very well with malformed strings and has to construct a string array).

All in all, I'd suggest you to overcome your aversion to regular expressions, since that situation is exactly what they're intended for :-) ...

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881453

You shouldn't need the first replace since you can just add 1 to the "(" position.

private static string DecipherUserName (string user) {           
    int start = user.IndexOf( "(" );
    if (start == -1)
        return user;
    return user.Substring (start+1).Replace( ")", string.Empty );
}

Upvotes: 9

Dillie-O
Dillie-O

Reputation: 29725

Since the IndexOf function will return -1 when the value doesn't exist, you could do things slightly different...

private static string DecipherUserName( string user )
{           
   int start = user.IndexOf( "(" );

   if (start > -1)
   {
      return user.Substring( start ).Replace( "(", string.Empty ).Replace( ")", string.Empty );
   }
   else
   {
      return user;
   }
}

Upvotes: 2

Related Questions