String Manipulation

  • Thread starter Thread starter Brian Hammer
  • Start date Start date
B

Brian Hammer

All, if I had a string that looked like:

"This user has a username of - brian hammer"
or
"This user has a username of - Any Number Of Characters"


How can I get the string that is right of the "- " for example, return just
brian hammer?

Thanks,
Brian
 
"This user has a username of - brian hammer"
or
"This user has a username of - Any Number Of Characters"


How can I get the string that is right of the "- " for example, return
just brian hammer?
You can use the Substring and IndexOf methods of the String class. Like
shown here:
string myString="This user has a username of - brian hammer";
string delimiter=" - ";
return myString.Substring(myString.IndexOf(delimiter)+delimiter.Length);

If you expect multiple '-' characters and want the string after the last '-'
character use the LastIndexOf method instead of the IndexOf method.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Thanks a bunch Anders Norås

Brian

Anders Norås said:
You can use the Substring and IndexOf methods of the String class. Like
shown here:
string myString="This user has a username of - brian hammer";
string delimiter=" - ";
return myString.Substring(myString.IndexOf(delimiter)+delimiter.Length);

If you expect multiple '-' characters and want the string after the last
'-' character use the LastIndexOf method instead of the IndexOf method.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Back
Top