String handling, trim a string in c#

Posted on 2009-04-20
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-ch\username\username\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
    LVL 74

    Expert Comment

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

    Expert Comment


    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 74

    Expert Comment

    by:käµfm³d 👽
    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

    return MyString.Substring(MyString.IndexOf("\"));

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
    Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now