471,317 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

string concatenation....

Hi,

I am a newbee to C# and i have a very simple problem. Basically I
have a string "servername\userid". I just want to get the userid part.
Don't know how. C# Gurus, can you show me a simple solution.

Jan 31 '06 #1
3 1302

Prabhu Dev wrote:
Hi,

I am a newbee to C# and i have a very simple problem. Basically I
have a string "servername\userid". I just want to get the userid part.
Don't know how. C# Gurus, can you show me a simple solution.


Sure.

string s = "servername\\userid";
string[] vals = s.Split(new char[]{'\\'});

vals will contain two string values

vals[0] = "servername"
vals[1] ="userid"

Matt

Jan 31 '06 #2
Prabhu Dev <pr*******@gmail.com> wrote:
I am a newbee to C# and i have a very simple problem. Basically I
have a string "servername\userid". I just want to get the userid part.
Don't know how. C# Gurus, can you show me a simple solution.


Three alternatives:

1) Use String.IndexOf to find the '\\' then String.Substring to get the
userid.

2) Use String.Split (with '\\' as the delimiter) then take the second
element of the returned array - checking that there *are* two elements,
of course

3) Use a regular expression and a capturing group.
I would advocate either of the first two myself - the first is slightly
more efficient. Any of the three will work though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 31 '06 #3
Thanks Guys.

Jan 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jonas Galvez | last post: by
20 posts views Thread by hagai26 | last post: by
3 posts views Thread by John Ford | last post: by
9 posts views Thread by Justin M. Keyes | last post: by
16 posts views Thread by Mark A. Sam | last post: by
12 posts views Thread by Richard Lewis Haggard | last post: by
34 posts views Thread by Larry Hastings | last post: by
10 posts views Thread by =?Utf-8?B?RWxlbmE=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.