Multiple string replace at one time - vb.net

I have this string abcd, and I want to replace the a with [a|b] and the b with [c|d]
I try many ways to do it, like
Dim varString As String = "abcd"
varString = varString.Replace("a", "[a|b]")
varString = varString.Replace("b", "[c|d]")
The result I get is
[a|[c|d]][c|d]cd
Instead I want it like this
[a|b][c|d]cd
The problem is every time I use the replace function it backs to change the values I already replaced before so I replaced a with [a|b] but then when I do my second command to replace the b it changes the b in [a|b] that I just changed and I don't want this.
I tried to use StringBuilder but it gives the same result.
Please advise me,

I solved the problem by making an array in this way
Dim NewCommand As String = "abcd"
For i = 0 To LikeCommand.Length - 1
If LikeCommand(i) = "a" Then
NewCommand += "[a|b]"
ElseIf LikeCommand(i) = "b" Then
NewCommand += "[c|d]"
Else
NewCommand += LikeCommand(i)
End If
Next
LikeCommand = NewCommand

Or just switch the logic up. But obviously I'm thinking you're using a basic example for a more complex question.
dim varString as string = "abcd"
varString = varString.Replace("b" ,"[c|d]")
varString = varString.Replace("a" ,"[a|b]")
That would get you the desired results.

Related

For Loop: changing the loop condition while it is looping

What I want to do is replace all 'A' in a string with "Bb". but it will only loop with the original string not on the new string.
for example:
AAA
BbAA
BbBbA
and it stops there because the original string only has a length of 3. it reads only up to the 3rd index and not the rest.
Dim txt As String
txt = output_text.Text
Dim a As String = a_equi.Text
Dim index As Integer = txt.Length - 1
Dim output As String = ""
For i = 0 To index
If (txt(i) = TextBox1.Text) Then
output = txt.Remove(i, 1).Insert(i, a)
txt = output
TextBox2.Text += txt + Environment.NewLine
End If
Next
End Sub
I think this leaves us looking for a String.ReplaceFirst function. Since there isn't one, we can just write that function. Then the code that calls it becomes much more readable because it's quickly apparent what it's doing (from the name of the function.)
Public Function ReplaceFirst(searched As String, target As String, replacement As String) As String
'This input validation is just for completeness.
'It's not strictly necessary.
'If the searched string is "null", throw an exception.
If (searched Is Nothing) Then Throw New ArgumentNullException("searched")
'If the target string is "null", throw an exception.
If (target Is Nothing) Then Throw New ArgumentNullException("target")
'If the searched string doesn't contain the target string at all
'then just return it - were done.
Dim foundIndex As Integer = searched.IndexOf(target)
If (foundIndex = -1) Then Return searched
'Build a new string that replaces the target with the replacement.
Return String.Concat(searched.Substring(0, foundIndex), replacement, _
searched.Substring(foundIndex + target.Length, searched.Length - (foundIndex + target.Length)))
End Function
Notice how when you read the code below, you don't even have to spend a moment trying to figure out what it's doing. It's readable. While the input string contains "A", replace the first "A" with "Bb".
Dim input as string = "AAA"
While input.IndexOf("A") > -1
input = input.ReplaceFirst(input,"A","Bb")
'If you need to capture individual values of "input" as it changes
'add them to a list.
End While
You could optimize or completely replace the function. What matters is that your code is readable, someone can tell what it's doing, and the ReplaceFirst function is testable.
Then, let's say you wanted another function that gave you all of the "versions" of your input string as the target string is replaced:
Public Function GetIterativeReplacements(searched As String, target As String, replacement As String) As List(of string)
Dim output As New List(Of String)
While searched.IndexOf(target) > -1
searched = ReplaceFirst(searched, target, replacement)
output.Add(searched)
End While
Return output
End Function
If you call
dim output as List(of string) = GetIterativeReplacments("AAAA","A","Bb")
It's going to return a list of strings containing
BbAAA, BbBbAA, BbBbBbA, BbBbBbBb
It's almost always good to keep methods short. If they start to get too long, just break them into smaller methods with clear names. That way you're not trying to read and follow and test one big, long function. That's difficult whether or not you're a new programmer. The trick isn't being able to create long, complex functions that we understand because we wrote them - it's creating small, simpler functions that anyone can understand.
Check your comments for a better solution, but for future reference you should use a while loop instead of a for loop if your condition will be changing and you're wanting to take that change into account.
I've made a simple example below to help you understand. If you tried the same with a for loop, you'd only get "one" "two" and "three" printed because the for loop doesn't 'see' that vals was changed
Dim vals As New List(Of String)
vals.Add("one")
vals.Add("two")
vals.Add("three")
Dim i As Integer = 0
While i < vals.Count
Console.WriteLine(vals(i))
If vals(i) = "two" Then
vals.Add("four")
vals.Add("five")
End If
i += 1
End While
If you do want to replace one by one instead of using the Replace function, you could use a while loop to look for the index of your search character/string, and then replace/insert at that index.
Sub Main()
Dim a As String = String.Empty
Dim b As String = String.Empty
Dim c As String = String.Empty
Dim d As Int32 = -1
Console.Write("Whole string: ")
a = Console.ReadLine()
Console.Write("Replace: ")
b = Console.ReadLine()
Console.Write("Replace with: ")
c = Console.ReadLine()
d = a.IndexOf(b)
While d > -1
a = a.Remove(d, b.Length)
a = a.Insert(d, c)
d = a.LastIndexOf(b)
End While
Console.WriteLine("Finished string: " & a)
Console.ReadLine()
End Sub
Output would look like this:
Whole string: This is A string for replAcing chArActers.
Replace: A
Replace with: Bb
Finished string: This is Bb string for replBbcing chBbrBbcters.
I was going to write a while loop to answer your question, but realized (with assistance from others) that you could just .replace(x,y)
Output.Text = Input.Text.Replace("A", "Bb")
'Input = N A T O
'Output = N Bb T O
Edit: There is probably a better alternative, but i quickly jotted this loop down, hope it helps.
You've said your new and don't fully understand while loops. So if you don't understand functions either or how to pass arguments to them, I'd suggest looking that up too.
This is your Event, It can be a Button click or Textbox text change.
'Cut & Paste into an Event (Change textboxes to whatever you have input/output)
Dim Input As String = textbox1.Text
Do While Input.Contains("A")
Input = ChangeString(Input, "A", "Bb")
' Do whatever you like with each return of ChangeString() here
Loop
textbox2.Text = Input
This is your Function, with 3 Arguments and a Return Value that can be called in your code
' Cut & Paste into Code somewhere (not inside another sub/Function)
Private Function ChangeString(Input As String, LookFor As Char, ReplaceWith As String)
Dim Output As String = Nothing
Dim cFlag As Boolean = False
For i As Integer = 0 To Input.Length - 1
Dim c As Char = Input(i)
If (c = LookFor) AndAlso (cFlag = False) Then
Output += ReplaceWith
cFlag = True
Else
Output += c
End If
Next
Console.WriteLine("Output: " & Output)
Return Output
End Function

for loop : string & number without keep adding &

I'm learning for loop and I cannot get this problem fixed.
The problems are in the following codes.
dim rt as integer = 2
dim i As Integer = 0
dim currentpg as string = "http://homepg.com/"
For i = 0 To rt
currentpg = currentpg & "?pg=" & i
messagebox.show(currentpg)
next
'I hoped to get the following results
http://homepg.com/?pg=0
http://homepg.com/?pg=1
http://homepg.com/?pg=2
'but instead I'm getting this
http://homepg.com/?pg=0
http://homepg.com/?pg=0?pg=0
http://homepg.com/?pg=0?pg=0?pg=0
Please help me
Thank you.
You probably need something like this:
Dim basepg as string = "http://homepg.com/"
For i = 0 To rt
Dim currentpg As String = basepg & "?pg=" & i
messagebox.show(currentpg)
Next
Although a proper approach would be to accumulate results into a List(Of String), and then display in a messagebox once (or a textbox/file, if too many results). You don't want to bug user for every URL (what if there are 100 of them?). They would get tired of clicking OK.
First of all, you went wrong while copying the output of the buggy code. Here is the real one.
http://homepg.com/?pg=0
http://homepg.com/?pg=0?pg=1
http://homepg.com/?pg=0?pg=1?pg=2
It does not work because currentpg should be a constant but it is changed on each iteration.
Do not set, just get.
MessageBox.Show(currentpg & "?pg=" & i)
Or you can use another variable to make it more readable.
Dim newpg As String = currentpg & "?pg=" & i
MessageBox.Show(newpg)
Also, your code is inefficient. I suggest you to change it like this.
Dim iterations As Integer = 2
Dim prefix As String = "http://homepg.com/?pg="
For index As Integer = 0 To iterations
MessageBox.Show(prefix & index)
Next

Remove "#" from String

Have an email, want to remove the first "#" symbol from it, to then make sure it doesn't have more then one in the second check. Here is currently how I'm doing it.
Dim tempEmail As String = ContactEmail
Dim count As Integer = 0
If tempEmail.IndexOf("#") <> -1 Then 'check for one
count += 1
tempEmail.Remove(tempEmail.IndexOf("#"), 1)
End If
If tempEmail.IndexOf("#") <> -1 Then 'check for two
count += 1
End If
If count = 1 Then
JustifyString(ContactEmail, 66, " ", LEFT_JUSTIFY)
Else
ContactEmail = BLANK_EMAIL
End If
But after debugging, I have found that it never actually removes the "#" symbol from the string from tempEmail. Why?
String is immutable. All String methods do not alter the String, but instead they create a new one and return it. Try this instead:
tempEmail = tempEmail.Remove(tempEmail.IndexOf("#"), 1)
Remove() returns a new string. It does not modify the original.
tempEmail = tempEmail.Remove(tempEmail.IndexOf("#"), 1)
As others have stated, strings are immutable in .NET. The Remove method returns a new string rather than changing the original object. Therefore you need to use:
tempEmail = tempEmail.Remove(tempEmail.IndexOf("#"), 1)
One quick way to determine whether the string contains multiple "#" symbols is via LINQ, rather than using IndexOf multiple times:
Dim input = "foo#bar.com"
Dim count = input.Count(Function(c) c = "#"c)
Console.WriteLine(count)
Then just check If count = 1 just as you do in your original If/Else block.
tempEmail.Remove(tempEmail.IndexOf("#"), 1)
This line creates a new string without the "#". You need to set tempEmail to be equal to the command:
tempEmail = tempEmail.Remove(tempEmail.IndexOf("#"), 1)
Strings are immutable. They don't change. To get the effect you want, change the line that reads:
tempEmail.Remove(tempEmail.IndexOf("#"), 1)
...to:
tempEmail = tempEmail.Remove(tempEmail.IndexOf("#"), 1)

How to use replace when making one variable = two others instead of just one

Okay this one might be a little tougher. I'm using VB that looks like this:
string = Replace(string.ToLower, chr(63), "A")
But I also want chr(63) = "B" as well, like this:
string = Replace(string.ToLower, chr(63), "B")
My problem is that when chr(63) is at the end of a string I need it to be B, and when it's not the end I need it to be A. I suppose that I can use an if/then/else statement. Is there a way to do this?
Example:
XXXXXchr(63)XXXXX = A
but
XXXXXXXXXXchr(63) = B
Thanks!
pseudo:
if (string[string.Length] == chr(63))
{
string[string.Length] = B
}
string = Replace(string.ToLower, chr(63), "A")
string = Replace(string.ToLower, chr(63), "A", 1, Len(string) - 1)
If Right(string, 1) = chr(63) then
Mid$(string, Len(string), 1) = 'B'
End if
Update: in response to comment:
VB String Functions
VB String Array Functions - Split, Join, Filter (very useful)
I haven't used Visual Basic since version 6, but it should be something like this:
If Robert.EndsWith(chr(63)) Then
Robert = Left(Robert, Robert.Length - 1) + "B"
End If
Then do the usual replacement with A.
This ought to do it
Dim s As String
Dim char63 As String = Convert.ToChar(63).ToString
If s.EndsWith(char63) Then
s = s.Substring(0, s.Length - 1) & "B"
End If
s = s.Replace(char63, "A")

Cant figure out how to merge variables vb.net

I am creating a for each loop to take the words from a string and place them each into a text box. The program allows for up to "9" variables What I am trying to attempt is.
Foreach word in Words
i = i +1
Varible & i = word
txtCritical1.Text = variable & i
any ideas on a way to make this work?
Have a look through the MSDN article on For Each. It includes a sample using strings.
https://msdn.microsoft.com/en-us/library/5ebk1751.aspx
So i went with a simple if statement this does the trick. Each text box is filled in.
Dim details As String = EditEvent.EditDbTable.Rows(0).Item(13).ToString()
Dim words As String() = details.Split(New Char() {"«"})
Dim word As String
For Each word In words
i = i + 1
v = word
If i = 1 Then
txtCritical1.Text = v
ElseIf i = 2 Then
txtCritical2.Text = v
ElseIf ....
ElseIf i = 9 then
txtCritical2.text = v
Else
....
End If
Next