Could someone please assist here? I have a program that reads a text file and copys the contents separated by a $ sign and writes the details to another new text file excluding the $ Sign. What I need assistance is to do a count and get the total records separate by the $ sign. Eg. ajasasaj$testing%878788%%$lkshhajsjah
for the above the total count would be 3. What I want is the 3 value. so how could I get that 3? Please assist at your earliest.

The split function will do the job:

Dim MyString As String = "ajasasaj$testing%878788%%$lkshhajsjah"

'This splits a the string, by the "$", into an array
Dim MySplitString() As String = MyString.Split("$")

'This gives the number of records
Dim Cnt As Integer = MySplitString.Count

'This joins the string back together without the "$"
Dim MyReturnString = String.Join("",MySplitString)

I'm not trying to step on any toes, I just wanted to offer another approach. Also love the way Regex works.

Imports System.Text.RegularExpressions

Module Module1

    Sub Main()

        Dim s As String = "ajasasaj$testing%878788%%$lkshhajsjah"

        Console.WriteLine("The Number Of Records in the line = " & NumberOfRecords(s, "$").ToString)
        Console.WriteLine("Records with the $ replaced with a blankspace ..." & vbCrLf & _
                          NewRecordLine(s, "$"))
    End Sub

    Private Function NumberOfRecords(ByVal records As String, ByVal splitOn As String) As Integer

        Dim r As Regex = New Regex(splitOn)
        Dim re() As String = r.Split(records)
        Return re.Count + 1

    End Function

    Private Function NewRecordLine(ByVal records As String, ByVal replace As String) As String

        replace = "\" & replace
        Return Regex.Replace(records, replace, " ")

    End Function

End Module
This article has been dead for over six months. Start a new discussion instead.