string concatenation....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prabhu  Dev

    #1

    string concatenation....

    Hi,

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

  • Matt

    #2
    Re: string concatenation.. ..


    Prabhu Dev wrote:[color=blue]
    > Hi,
    >
    > I am a newbee to C# and i have a very simple problem. Basically I
    > have a string "servername\use rid". I just want to get the userid part.
    > Don't know how. C# Gurus, can you show me a simple solution.[/color]

    Sure.

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

    vals will contain two string values

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

    Matt

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: string concatenation.. ..

      Prabhu Dev <prabhudev@gmai l.com> wrote:[color=blue]
      > I am a newbee to C# and i have a very simple problem. Basically I
      > have a string "servername\use rid". I just want to get the userid part.
      > Don't know how. C# Gurus, can you show me a simple solution.[/color]

      Three alternatives:

      1) Use String.IndexOf to find the '\\' then String.Substrin g 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 - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Prabhu  Dev

        #4
        Re: string concatenation.. ..

        Thanks Guys.

        Comment

        Working...