Results 1 to 6 of 6

Thread: [RESOLVED] Split string

  1. #1

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Resolved [RESOLVED] Split string

    Hey there. I have a range of strings (Basically filenames) and I want to strip them down.

    Basically I load a list of files (path/clubs/) and it lists them into a list box as club1.adf club2.adf etc.

    I then select club1.adf for example from the list box, and hit a button. The button then loads path/club1 into my application.

    So basically I want to split the .adf from the club1. Something like the following, I imagine?

    X = club1.adf
    'splitting here'
    Y = club1
    listbox1.items.add(Y)

    I hope this sounds simple!

  2. #2
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Split string

    Have you tried checking the MSDN library for the split function? Even spending 30 seconds in google would reveal the answer to how to use split.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  3. #3

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Split string

    Hey, I did do that. And came up with this code:

    Dim zyx As String
    zyx = Split(Filename.Name, " .adf ", , CompareMethod.Text

    However, I get the error "Error 6 Value of type '1-dimensional array of String' cannot be converted to 'String'"

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Split string

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim files() As String = IO.Directory.GetFiles(path, "*.adf")
    5.         Dim items() As listItem = Array.ConvertAll(files, New Converter(Of String, listItem)(AddressOf String2listItem))
    6.         ListBox1.Items.AddRange(items)
    7.     End Sub
    8.  
    9.     Private Function String2listItem(ByVal item As String) As listItem
    10.         Return New listItem With {.display = IO.Path.GetFileNameWithoutExtension(item), .fullPath = item}
    11.     End Function
    12.  
    13.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    14.         MsgBox(DirectCast(ListBox1.SelectedItem, listItem).fullPath)
    15.     End Sub
    16. End Class
    17.  
    18. Public Class listItem
    19.     Public display As String
    20.     Public fullPath As String
    21.     Public Overrides Function ToString() As String
    22.         Return display
    23.     End Function
    24. End Class

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Split string

    alternatively, instead of a converter function, you can use a lambda function:

    vb Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim files() As String = IO.Directory.GetFiles(path, "*.adf")
    4.         Dim items() As listItem = Array.ConvertAll(files, New Converter(Of String, listItem) _
    5.         (Function(item) New listItem With {.display = IO.Path.GetFileNameWithoutExtension(item), .fullPath = item}))
    6.         ListBox1.Items.AddRange(items)
    7.     End Sub
    8.  
    9.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    10.         MsgBox(DirectCast(ListBox1.SelectedItem, listItem).fullPath)
    11.     End Sub
    12. End Class
    13.  
    14. Public Class listItem
    15.     Public display As String
    16.     Public fullPath As String
    17.     Public Overrides Function ToString() As String
    18.         Return display
    19.     End Function
    20. End Class

  6. #6

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Split string

    This is 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