PDA

Click to See Complete Forum and Search --> : [2.0] How to extract a portion of a variable in C#


bnathvbdotnet
Sep 13th, 2006, 10:45 AM
Its quite easy in VB (Instr function).

I have username stored in a string variable. the content of the variable is "//xx.com/nathb". I would like to extract only "nathb" portion . How can I do that in C#?

thanks

mendhak
Sep 13th, 2006, 11:03 AM
There are many string manipulation functions in .NET: .Substring, .IndexOf(), etc. which you can use to get what you need. In your case, you appear to want the text from the string after the last forward-slash.

string original = "//xx.com/nathb";
string extracted = original.substring(original.LastIndexOf("/"));

May need a bit of tweaking.

jmcilhinney
Sep 13th, 2006, 05:48 PM
That string is a path so you can use the IO.Path class:string path = "//xx.com/nathb";
string userName = System.IO.Path.GetFileName(path);That's what I'd do in VB too.

mar_zim
Sep 14th, 2006, 02:01 AM
or you could use split function.

DNA7433
Sep 14th, 2006, 02:31 AM
Or regular expressions :D

mendhak
Sep 14th, 2006, 07:55 AM
There are four ways to skin string x = "a dead cat";

wossname
Sep 14th, 2006, 03:25 PM
Serialize the dead cat to a memory stream, move the stream position to one layer below the Skin object and then deserialize again. Optionally Deserialize the skin at a later point if needed.