Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to replace the first "_" and second "_" character with ":" and "@"from many different strings.

Example strings are like this:

test_test_0.0.0.0
test5_test5_1.1.1.1
testing_testing_0.1.2.3
blabla_blabla_0.5.4.3

How can I replace these example strings in an easy way into:

test:test@0.0.0.0
test5:test5@1.1.1.1
testing:testing@0.1.2.3
blabla:blabla@0.5.4.3

I have searched but couldn't find this specific question
Many thanks

What I have tried:

vbnet
string.Replace("_", ":").Replace("_", "@")
Posted
Updated 13-Mar-22 6:55am

You could try splitting the string using underscores and then rejoining with the desired characters.


Dim temp As String = "testing_testing_0.1.2.3"
Dim result As String
Dim splitarray() As String

splitarray = temp.Split("_")

If splitarray.Length = 3 Then
    result = splitarray(0) + ":" + splitarray(1) + "@" + splitarray(2)
End If
 
Share this answer
 
Comments
Maciej Los 13-Mar-22 8:45am    
5ed!
Tony Hill 13-Mar-22 9:00am    
Thanks
benngia 14-Mar-22 13:20pm    
Tony your code is working perfectly, Thank you for this, I am almost there.
Now I have to figure out how to add the "result" as Listbox1.items..
I need help with this..
Msgbox(result) is working and showing perfect output.

For Each item In ListBox1.Items
Dim result As String
Dim splitarray() As String

splitarray = item.Split("_")

If splitarray.Length = 3 Then
result = splitarray(0) + ":" + splitarray(1) + "@" + splitarray(2)

MsgBox(result) 'just to see what is happening

'ListBox1.Items.Add(result) not working "yet" :(
'ListBox1.Refresh()

End If

Next
Replace changes all instances - that doesn't help you at all.
You could use a Regex: .NET Regular Expressions | Microsoft Docs[^] but that could take some time as they aren;t the simplest thing to learn if you haven't met them before, though Expresso[^] may help - it's free, and it examines and generates Regular expressions.

The simplest solution is to use the string.IndexOf overloads to get you the position of the two replaceable characters in your string, then use String.Substring[^] to break the string around them and concatenation to form a new string with the replaces characters.
 
Share this answer
 
Comments
Maciej Los 13-Mar-22 9:18am    
5ed!
Using Regex.Replace Method[^] :

C#
string[] lines = {"test_test_0.0.0.0",
    "test5_test5_1.1.1.1",
    "testing_testing_0.1.2.3",
    "blabla_blabla_0.5.4.3"};

string pattern = @"(\w+)(_)(\w+)(_)";
string replacement = "$1:$3@";
foreach(string s in lines)
{
    string sOut = Regex.Replace(s, pattern, replacement, RegexOptions.IgnoreCase);
    Console.WriteLine(sOut);
}


If you want to see how Regex work: regex101: build, test, and debug regex[^]
 
Share this answer
 
Comments
Tony Hill 13-Mar-22 13:23pm    
+5
Maciej Los 13-Mar-22 13:32pm    
Thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900