I am reading a text file and I need to replace a few areas with new text that I marked with {0}, {1}, {2}, {3}, {4}, {5}, {6}.
So I load in the text, save it to a string variable, and then use String.Replace but it’s not working.
For i As Integer = 0 To 6
fileText.Replace("{" & i & "}", DisplayStudentData(i))
Next
And DisplayStudentData looks like this:
Protected Function DisplayStudentData(ByVal itemNumber As Integer) As String
Dim dsItem As String = ""
If itemNumber <> -1 Then
Select Case itemNumber
Case 0
dsItem = "testFirstName"
Case 1
dsItem = "testTitle"
Case 2
dsItem = "testClass"
Case 3
dsItem = "testTeacher"
Case 4
dsItem = "testDept"
Case 5
dsItem = "testEmail"
Case 6
dsItem = "testPhone"
End Select
End If
Return dsItem
End Function
It seems like the above should work, but it doesn’t.
Any help would be greatly appreciated.
Thanks!
Ok I figured it out…
I put all the data items into an array, then did this after loading the text file:
fileText = String.Format(fileText, dArr(0), dArr(1), dArr(2), dArr(3), dArr(4), dArr(5), dArr(6))
Is this a good way of doing it?
Replace returns a new string. It does not modify the string from which it was called.
You could also do this:
Why isn’t DisplayStudentData just an array in the first place (which would make string.Format() even easier)?