|
-
May 15th, 2010, 02:30 PM
#1
Thread Starter
Addicted Member
[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!
-
May 15th, 2010, 02:36 PM
#2
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.
-
May 15th, 2010, 02:52 PM
#3
Thread Starter
Addicted Member
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'"
-
May 15th, 2010, 03:00 PM
#4
Re: Split string
try this:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim files() As String = IO.Directory.GetFiles(path, "*.adf")
Dim items() As listItem = Array.ConvertAll(files, New Converter(Of String, listItem)(AddressOf String2listItem))
ListBox1.Items.AddRange(items)
End Sub
Private Function String2listItem(ByVal item As String) As listItem
Return New listItem With {.display = IO.Path.GetFileNameWithoutExtension(item), .fullPath = item}
End Function
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(DirectCast(ListBox1.SelectedItem, listItem).fullPath)
End Sub
End Class
Public Class listItem
Public display As String
Public fullPath As String
Public Overrides Function ToString() As String
Return display
End Function
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 15th, 2010, 03:10 PM
#5
Re: Split string
alternatively, instead of a converter function, you can use a lambda function:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim files() As String = IO.Directory.GetFiles(path, "*.adf")
Dim items() As listItem = Array.ConvertAll(files, New Converter(Of String, listItem) _
(Function(item) New listItem With {.display = IO.Path.GetFileNameWithoutExtension(item), .fullPath = item}))
ListBox1.Items.AddRange(items)
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(DirectCast(ListBox1.SelectedItem, listItem).fullPath)
End Sub
End Class
Public Class listItem
Public display As String
Public fullPath As String
Public Overrides Function ToString() As String
Return display
End Function
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 15th, 2010, 03:17 PM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|