[Webinar] Learn how to a build a cloud-first strategyRegister Now


String handling, trim a string in c#

Posted on 2009-04-20
Medium Priority
Last Modified: 2013-12-17

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.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.
Question by:olca02
  • 2
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24184140
System.IO.Path.GetFilename("domain\username") might work.

Expert Comment

ID: 24184169

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

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24184172
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 ;)
LVL 32

Accepted Solution

Daniel Wilson earned 2000 total points
ID: 24184191
return MyString.Substring(MyString.IndexOf("\"));


You may have to put a +1 in there ... not sure.  But that should get you really close.

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question