Getting strings [solved]

Posts 1–9 of 9 · Page 1 of 1
Getting strings [solved]
Hi guys. I need some help about this.

I would like to collect the words on the sentence and they'll be added at the listbox.
Also getting texts after some extra characters...

A sample pic here so I hope someone could help me about it.
*Image removed

Thanks in advance.
[highlight=vbnet]Imports System.Text.RegularExpressions[/highlight]

[highlight=vbnet]For Each Match M in Regex.Split(Textbox1.Text,"\w")
Listbox1.Items.Add(M.Value)
Next[/highlight]
I think its like this. (Found it on the snippets)
Code:
Public Function Split(ByVal [String] As String, Optional ByVal DontAccountQuotes As Boolean = False, Optional ByVal Separator As String = " ") As String()
            If DontAccountQuotes Then
                Return [String].Split(Separator)
            Else
                Dim words As String() = [String].Split(Separator)
                Dim newwords As New ArrayList
                For i As Integer = 0 To words.Length - 1
                    If words(i).StartsWith("""") Then
                        Dim linked As New ArrayList
                        For y As Integer = i To words.Length - 1
                            If words(y).EndsWith("""") Then
                                linked.Add(words(y).Substring(0, words(y).Length - 1))
                                i = y
                                Exit For
                            Else
                                If words(y).StartsWith("""") Then words(y) = words(y).Substring(1)
                                linked.Add(words(y))
                            End If
                        Next
                        Dim tmp(linked.Count - 1) As String
                        linked.CopyTo(tmp)
                        If Not linked.Count = 0 Then newwords.Add(String.Join(Separator, tmp))
                        linked.Clear()
                        Erase tmp
                    Else
                        newwords.Add(words(i))
                    End If
                Next
                Dim tmp2(newwords.Count - 1) As String
                newwords.CopyTo(tmp2)
                newwords.Clear()
                Return tmp2
            End If
        End Function
but this code only extract words between spaces... I still need how to extract the words that ends with ().
ListBox1.Items.AddRange(System.Text.RegularExpress ions.Regex.Split(TextBox1.Text, "\(\)"))
That seems to work but there is a space that is included.
ListBox1.Items.AddRange(System.Text.RegularExpress ions.Regex.Split(TextBox1.Text, "\(\) *"))

lol, fixed. ;D
[highlight=vb.net]
Module StringExtensions
<Runtime.CompilerServices.Extension()> _
Public Function ExtendedSplit(ByVal [Me] As String, ByVal delimiter As String, Optional ByVal replace As String() = Nothing) As String()
Dim rawSplit As String() = [Me].Split(New String() {delimiter}, StringSplitOptions.RemoveEmptyEntries)
If replace is Nothing Then Return rawSplit
For i As Integer = 0 To rawSplit.Length - 1
For Each r As String In replace
rawSplit(i) = rawSplit(i).Replace(r, "")
Next
Next
Return rawSplit
End Function
End Module
[/highlight]

Then I'd just do:

[highlight=vb.net]
'for the first one:
Dim example1 As String = "Sample Text String"
ListBox1.Items.AddRange(example1.ExtendedSplit(" "))

'for the second one:
Dim example2 As String = "One() Two() Three()"
ListBox1.Items.AddRange(example2.ExtendedSplit(" ", New String() { "(", ")" }))
[/highlight]
Thanks Jason. That code is really great..
#My problem is solved.
NP, marked solved.
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?