reading a string from the right until...

I thought this would be simple to figure out (and perhaps it is), but I’m not finding it. I want to extract the last part of a string and save it in a variable. The string lengths are never the same, but there are consistent separators, as below:

home\work\school\neighbor

I need to save “neighbor” in a variable. My idea was to simply read the string from right to left and stop at the first instance of “\” but I am having trouble finding the proper way to do this.

BTW, I’m using VB 2005 express


Answer this question

reading a string from the right until...

  • Grant Holliday

    Spotty~

     

    Sure enough...that works. Thank you very much.

    Now I just have to dissect it and make sure I know how/why...

    Thanks again.

     

     and to you, ReneeC


  • c y h

     

     

    Yes there are a lot of ways of doing this  but you did it the hard way spotty

     

    private function ResString (byval instring as string) as string

    return instring.substring(instring.lastindexof("/")-1)

    End Function

     

    Should do it.....



  • AncientTones

    The function should work on a form in the same way as the console application.

    Obviously the surrounding code Module .... End Module is not there if it is in a form.

    So if you take the function and paste it into the form this should work. This was what I originally had working.


    Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim s As String = "home\work\school\neighbor"
    Dim Item As String = extractlastItem(s, "\")
    MsgBox(Item)
    End Sub

    Function ExtractLastItem(ByVal s As String, ByVal character As Char) As String

    Dim s1() As String
    s1 = s.Split(character)
    If s1.Length > 0 Then
    Return s1.GetValue(s1.Length - 1)
    Else
    Return ""
    End If
    End Function

    End Class


    And yes Renee you are correct. This split enables you to have an array which you could return back any/all/last of the words separated by the character. In this case I returned the last. At first I was thinking somewhat of the old VB Classic way when I would probably have stepped through each character in reverse to find the separator and then done and instr.

    But thinking if they have multiple characters in the string - they may want to access each item in the string not just the last. Just thinking maybe a little ahead.

    Like all things in development there are numerous ways of doing things. ;-)


  • Techi Vignesh

    Spotty~

     

    Hey…that did work. Thanks

     

    Can you answer a question though

     

    Why does it not work in a form (or does it ). I got it to work in a new console, but when attempting to place the same code in an ongoing form build, I didn’t get the same result.

     

    (I don't know why my text (in this forum) is showing up as different sizes...is it that way on you monitor too wierd)


  • zh

    Sure that can be done.... There are numerous ways of doing this.

    Heres a function which will do just what you want. Create a console application and paste this code in and you should be good to go.

    Module Module1
    Public Sub Main
    Dim s As String = "home\work\school\neighbor"
    Dim Item As String = extractlastItem(s, "\")
    MsgBox(Item)
    End Sub

    Function ExtractLastItem(ByVal s As String, ByVal character As Char) As String
    Dim s1() As String
    s1 = s.Split(character)
    If s1.Length > 0 Then
    Return s1.GetValue(s1.Length - 1)
    Else
    Return ""
    End If
    End Function
    End Module



  • reading a string from the right until...