Link to home
Start Free TrialLog in
Avatar of olca02
olca02

asked on

String handling, trim a string in c#

Hi.

I have some difficulties finding the right technique using strings in c# i hope somebody can help.

I have several domain\usernames which i need to have left trimmed. Right now the string is domain\username i only want the username to be visible. My issues is that the domain is not having the same length each time

domain-ch\username
domain.com\username
domain.it\username etc.
What do i need to do, for just ending up with the username and get rid of domain..\. Thanx every help is appreciated.
Avatar of kaufmed
kaufmed
Flag of United States of America image

System.IO.Path.GetFilename("domain\username") might work.
Avatar of BalkisBr
BalkisBr

Hi,

if you are working with strings use the Split method, like this:
//creating the string
string Test = @"domain\user";
//creating the split character
char[] split  = {'\\'};
//the [1] is to pick up the second part
// [0] = domain
// [1] = user
string user = Test.Split(split)[1];

Open in new window

As an additional note, you might want to comment what is going on when you use that function as you are not technically parsing a filename ;)
ASKER CERTIFIED SOLUTION
Avatar of Daniel Wilson
Daniel Wilson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial