CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2010
    Posts
    1

    Return Right characters of String up to a /

    I am trying to parse the following to get all characters to the right of the last "/". Can someone help me?

    http://www.mydomain.com/abc

    would return 'abc'

    http://www.mydomain.com/abc/MyOtherFolder/Thg3kd

    Would return 'Thg3kd'

    Thanks,

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Return Right characters of String up to a /

    use the String.LastIndexOf method + String.Substring or the String.Split method and get the last item from the returned array.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Feb 2010
    Posts
    7

    Re: Return Right characters of String up to a /

    string originalString = "http://www.mydomain.com/abc/MyOtherFolder/Thg3kd";
    string newString = "";
    int length = originalString.Length;
    for (int i = 0; i < length; i++)
    {
    if (originalString[i] == '/')
    {
    newString = "";
    }
    else
    {
    newString = newString + originalString[i];
    }
    }

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Return Right characters of String up to a /

    Quote Originally Posted by @kimlundgren_ View Post
    string originalString = "http://www.mydomain.com/abc/MyOtherFolder/Thg3kd";
    string newString = "";
    int length = originalString.Length;
    for (int i = 0; i < length; i++)
    {
    if (originalString[i] == '/')
    {
    newString = "";
    }
    else
    {
    newString = newString + originalString[i];
    }
    }
    not bad

    what about the code tags? don't you like them?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  5. #5
    Join Date
    Feb 2010
    Posts
    7

    Re: Return Right characters of String up to a /

    Quote Originally Posted by memeloo View Post
    not bad

    what about the code tags? don't you like them?
    Thx
    what code tags? Im really new here :P started my acc last night.

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Return Right characters of String up to a /

    Quote Originally Posted by @kimlundgren_ View Post
    Thx
    what code tags? Im really new here :P started my acc last night.
    Here I'm just using the HTML tags to make the CODE tags visible...
    HTML Code:
     The [CODE][/CODE] tags.
    P.S. It's a nice solution - parsing style, but for what the OP wanted to do, I think String.Split() would do fine; don't get me wrong - what you suggested is perfectly valid, and, what's most important, it works, it's just that, in case that there is a lot of this data to be processed, a potential problem could come from the fact that the String class is immutable - every assignment operation creates a new instance which replaces the original instance. This is why, in such cases, it is recommended for the coder to use the StringBuilder class. Also, the speed could be impacted because the indexer returns a char, which must be converted to a string in each iteration.

  7. #7
    Join Date
    Feb 2010
    Posts
    7

    Thumbs up Re: Return Right characters of String up to a /

    Quote Originally Posted by TheGreatCthulhu View Post
    Here I'm just using the HTML tags to make the CODE tags visible...
    HTML Code:
     The [CODE][/CODE] tags.
    P.S. It's a nice solution - parsing style, but for what the OP wanted to do, I think String.Split() would do fine; don't get me wrong - what you suggested is perfectly valid, and, what's most important, it works, it's just that, in case that there is a lot of this data to be processed, a potential problem could come from the fact that the String class is immutable - every assignment operation creates a new instance which replaces the original instance. This is why, in such cases, it is recommended for the coder to use the StringBuilder class. Also, the speed could be impacted because the indexer returns a char, which must be converted to a string in each iteration.
    thx

    and yes your defenitly right on that, mine wasn´t the best solution. next time Im in this situation I shall try String.Split().

  8. #8
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Return Right characters of String up to a /

    Incidentally, this is an opportunity to write some extensions (.Net 3.x+) to the String class. So you can do more readable things like:
    Code:
    String a = "This is some string of characters";
    String b = a.Left(4); // This
    String c = a.Mid(7, 11); // some string   ....  character 7 for 11 characters
    String d = a.Right(10); // characters
    If you write your own extensions you can overcome the short falls of the Substring(..) method (where it requires indices to actually be in the String), which can be confusing and lead to horrible looking code like:

    Code:
    String b = a.Substring(a.Length - 10, 10);
    Which crashes if 'a' is less than 10 characters in length. Whereas using an extension method you can choose to return 'up to 10' characters etc.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  9. #9
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Return Right characters of String up to a /

    You can use regular expressions:
    Code:
    string s = "http://www.mydomain.com/abc";
    Match m = Regex.Match(s, ".*/(.*)$");
    string s = m.Groups[1].Value // 0 is whole string
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

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