Results 1 to 7 of 7

Thread: [2.0] How to extract a portion of a variable in C#

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    [2.0] How to extract a portion of a variable in C#

    Its quite easy in VB (Instr function).

    I have username stored in a string variable. the content of the variable is "//xx.com/nathb". I would like to extract only "nathb" portion . How can I do that in C#?

    thanks

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] How to extract a portion of a variable in C#

    There are many string manipulation functions in .NET: .Substring, .IndexOf(), etc. which you can use to get what you need. In your case, you appear to want the text from the string after the last forward-slash.

    Code:
    string original = "//xx.com/nathb";
    string extracted = original.substring(original.LastIndexOf("/"));
    May need a bit of tweaking.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] How to extract a portion of a variable in C#

    That string is a path so you can use the IO.Path class:
    Code:
    string path = "//xx.com/nathb";
    string userName = System.IO.Path.GetFileName(path);
    That's what I'd do in VB too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: [2.0] How to extract a portion of a variable in C#

    or you could use split function.

  5. #5
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: [2.0] How to extract a portion of a variable in C#

    Or regular expressions
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] How to extract a portion of a variable in C#

    There are four ways to skin string x = "a dead cat";

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [2.0] How to extract a portion of a variable in C#

    Serialize the dead cat to a memory stream, move the stream position to one layer below the Skin object and then deserialize again. Optionally Deserialize the skin at a later point if needed.
    I don't live here any more.

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