CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    13

    Unhappy C# substring type method?

    Is there an easy way to get a substring of a string? I am calling a method that returns back an array of values that look similar to:

    keys[0] = "All|All";
    keys[1] = "Admin|Admin";
    keys[2] = "Shop Floor|Shop Floor";
    keys[3] = "Development|Development";
    keys[4] = "Front Desk|Front Desk";

    When I receive the results, I want to break out the results so that All|All becomes All and Admin|Admin becomes Admin. Is there an easy way to get rid of everything after the "|"?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C# substring type method?

    Code:
    key[n].Split('|')[0]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Re: C# substring type method?

    Can also do this way... though CPUWizard has more elegant solution

    Code:
    string[] q = Regex.Split("All|All", "[|]");
    MessageBox.Show(q[1].ToString().Trim());
    Last edited by MMH; November 12th, 2008 at 01:12 PM.
    Regards,
    MMH
    Rate my post if you find it usefull.

  4. #4
    Join Date
    Nov 2008
    Posts
    13

    Re: C# substring type method?

    Its perfect. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured