Replace() i-th char in vb

newbeee

Member
Joined
Sep 27, 2007
Messages
19
Programming Experience
Beginner
Is there a way to format a tab delimited array of string '1328 David Barr X...' to display in a data table with strings of 'FolderID, Firstname LastName, Status'?

That is replacing 1st & 3rd tab with a comma and all X’s, 1’s, 3’s with Cancelled, No Show with Reserve, No Show without Reserve.

For example:
1328, David Barr, Cancelled
123, Lala Land, No Show with Reserve
456, Santa Clause, No Show without Reserve


I can’t simply use replace(“X”, “Cancelled”) or replace(vbtab, “,”) because if there is an “x” somewhere in the name or the tab between the first & last name, it’ll replace also. Does vb has a replace from an i-th char? Keep in mind the result string is actually an array of string separated by a tab, not just a string.


Thanks in advance
 
Split the string by vbTab, change the 3rd array element with corresponding value:
VB.NET:
Dim s As String = "1328" & vbTab & "David Barr" & vbTab & "X" 'test data
Dim fields() As String = s.Split(vbTab)
Select Case fields(2)
    Case "X"
        fields(2) = "Cancelled"
    Case "1"
    Case "3"
End Select
s = String.Join(", ", fields)
 
I'm able to get close to what I want. However, because we use String.Join(", ", fields), the result is ending up in:
1328, David, Barr, Cancelled
123, Lala, Land, No Show with Reserve
456, Santa, Clause, No Show without Reserve

That is because David is taken from a FirstName field and Barr is taken from a LastName field which is separated by a tab. Thus, using String.Join automatically put a comma between the two. How can I skip the comma between First and Last?

For i As Integer = 0 To strResult.Length - 1
tempStr = Split(strResult(i), vbTab)
Select Case tempStr(3)
Case "X"
tempStr(3) = "Cancelled"
Case "0"
tempStr(3) = "No Show With Reserve"
Case "1"
tempStr(3) = "No Show Without Reserve"
Case "3"
tempStr(3) = "Cancelled"
End Select
strResult(i) = String.Join(", ", tempStr)
Next i

Thanks!
 
i'd do something like:
VB.NET:
Dim repl as New Dictionary(Of String, String)
repl("x") = "Cancelled"
...



For Each line As String in File.ReadAllLines("C:\myfile.txt")
  Dim ary()  as String = line.Split(vbTab)

  myTypedDataTable.AddWhateverRow(line(0), line(1), repl(line(2)))

Next line

myTypedDatatable is an isntance of a typed datatable you made with the dataset designer to take your data
 
Back
Top