Am working on a project right now and almost stuck at a point. Say, I have a text document with contents like:
abc,123.xyz;praise;end,file,clear

Now I want abc , 123 . xyz ; praise ... in an array. I used split method with array of characters. Though I retrieved abc,123,xyz,praise,end,file,clear I couldn't retrieve , . ; ; , , ,. I need every different characters in text file.

I imported the files to a rich text document control using file dialog. I want the the string in a variable say myText (it is an array)

myText(0)=abc
myText(1)=,
mytext(2)=123
myText(3)=.
myText(4)=xyz
and soon

Any help soon will be appreciated. Thanks!

Parse the string char by char.
As long as it is a letter put it in myText(n), if no letter or digit found like ';' increase n and place that char in the next myText.

you mean to use char.Parse method?

Console.WriteLine(Char.Parse("abc,123.xyz;praise;end,file,clear"))

I get this error:

String must be exactly one character long.

No I mean looping through the string:

string str = "abc,123.xyz;praise;end,file,clear";
int index = 0;
for (int i = 0; i < str.Length; i++)
{
   //add str[i] to myText[index] as long as it is a letter or digit
   //increment index if punctuation encountered etc.
}

Char.Parse accepts strings of only one char long, hence your error message.

You could use regular expressions as follows

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'the pattern matches the longest string of letters and/or digits

        Dim rex As New Regex("[a-z|A-Z|0-9]+")
        Dim str As String = "abc,123.xyz;praise;end,file,clear"

        For Each m As Match In rex.Matches(str)
            Debug.WriteLine(": " & m.Value)
        Next

    End Sub

End Class
commented: Indeed an option if you know regex well! Perhaps I should start learning. +15

@Reverend Jim

But usage of regular expressions again produces me the same result. I couldn't fetch the punctuation marks

Imports System.Text.RegularExpressions
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim rex As New Regex("[a-z|A-Z|0-9|.|]+")
        Dim str As String = "abc,123.xyz;Praise;end,file,clear"
        For Each m As Match In rex.Matches(str)
            TextBox1.Text &= vbNewLine & m.Value
        Next
    End Sub
End Class

is my code and the result is:

abc
123.xyz
Praise
end
file
clear

in the textbox..

If you need the punctuation marks then you are better off doing as ddanbe says - char by char. Usually, puntuation marks are used as delimiters and are not considered as part of the data. In that case you get

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim rex As New Regex("[a-z|A-Z|0-9]")
        Dim str As String = "abc,123.xyz;praise;end,file,clear"
        Dim tok As String = ""

        For Each ch As Char In str.ToCharArray

            If rex.IsMatch(ch) Then
                'letter or digit - add it to the token string
                tok &= ch
            Else
                'punctuation - display two tokens and clear string
                Debug.WriteLine(tok)
                Debug.WriteLine(ch)
                tok = ""
            End If

        Next

        If tok.Length > 0 Then Debug.WriteLine(tok)

    End Sub

End Class

@Riteman, it is required that you also store the separator(, . ; etc) on array? If not you can try the code below,

        Dim str As String = "abc,123.xyz;praise;end,file,clear"
        Dim punct As Char() = {",", ".", ";"}
        Dim collectedArr As String()

        collectedArr = str.Split(punct)

Output:

collectedArr(0) = abc
collectedArr(1) = 123
collectedArr(2) = praise
collectedArr(3) = end
So on ...

@Reverand Jim

That's the exact thing I need! Thanks for your mighty code! That really worked for me! Thanks a lot!

So this is my code snippet:

Imports System.Text.RegularExpressions
Public Class Form1
    Dim myText(100) As String      'My Stack
    Dim i As Integer = 0        'Incrementer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim rex As New Regex("[a-z|A-Z|0-9]")
        Dim str As String = "abc,123.xyz;praise;end,file,clear"
        Dim tok As String = ""
        For Each ch As Char In str.ToCharArray
            If rex.IsMatch(ch) Then
                'letter or digit - add it to the token string
                tok &= ch
            Else
                'punctuation - display two tokens and clear string
                TextBox1.Text &= tok & vbNewLine
                TextBox1.Text &= ch & vbNewLine
                myText(i) = tok
                i += 1
                myText(i) = ch
                i += 1
                tok = ""
            End If
        Next
        If tok.Length > 0 Then
            TextBox1.Text &= tok & vbNewLine
            myText(i) = tok
            i += 1
        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For a As Integer = 0 To 99
            TextBox2.Text &= a & " : " & myText(a) & vbNewLine
            If myText(a) = "praise" Then
                MsgBox("Praise God")
            End If
        Next
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.