Click here to Skip to main content
15,914,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string as follows

MDSP:TSK:Demo:MDSR:Demo:::LAT:Test:P:2.3:

Now i have to replace LAT between 7th ":" and 8th ":" to ACK.

i.e.

MDSP:TSK:Demo:MDSR:Demo:::ACK:Test:P:2.3:


Colud you please let me know this.
Posted
Comments
bbirajdar 19-Jul-12 8:04am    
Write a regular expression

Try this:
VB
Dim mystr As String = "MDSP:TSK:Demo:MDSR:Demo:::LAT:Test:P:2.3:"
mystr.Replace("LAT", "ACK")
 
Share this answer
 
Comments
jagadeesh kumar adabala 28-Jul-12 6:05am    
i could n't use like this.
mystr.Replace("LAT", "ACK")

Because we don't know what stirng will be there in next message. it will never be the same for next time....
Not the nicest solution of filtering out the interesting part, but it works:
VB
Dim newParam As String = "ACK"
Dim test As String = "MDSP:TSK:Demo:MDSR:Demo:::LAT:Test:P:2.3:"
Dim nStartExchange As Integer = test.IndexOf("::") + 3
Dim res As String = test.Substring(nStartExchange, test.Length - nStartExchange)
Dim nEndExchange As Integer = res.IndexOf(":"C) + nStartExchange
Dim result As String = test.Substring(0, nStartExchange) & newParam & test.Substring(nEndExchange)
MessageBox.Show(result)

I think you could also try using regular expressions in this case, but the above code works quite well for the string that you wanted to modify.

Another possible solution:
VB
Dim newParam As String = "ACK"
Dim test As String = "MDSP:TSK:Demo:MDSR:Demo:::LAT:Test:P:2.3:"
Dim results As String() = test.Split(":"C)
Dim final As String = ""
results(7) = newParam
For i As Integer = 0 To results.Length - 1
    final += results(i)
    If i <> results.Length - 1 Then
        final += ":"
    End If
Next
MessageBox.Show(final)

Hope it helps!
 
Share this answer
 
v4
1. simply use replace function,
VB
Dim str = "MDSP:TSK:Demo:MDSR:Demo:::LAT:Test:P:2.3:"
str = str.Replace("LAT","ACK")


2. if u want to change string between 7th ":" & 8th ":" (Incase "LAT" word is not fixed for replacement."
VB
Dim str = "MDSP:TSK:Demo:MDSR:Demo:::LAT:Test:P:2.3:"
               Dim colon_start_index = 0
               Dim colon_start_Occurance = 0
               For o As Integer = 0 To str.LastIndexOf(":") - 1
                   If colon_start_Occurance = 7 Then
                       Exit For
                   Else
                       colon_start_index = str.IndexOf(":", colon_start_index) + 1
                       colon_start_Occurance += 1
                   End If
               Next
               str = str.Replace(str.Substring(colon_start_index, str.IndexOf(":", colon_start_index) - (colon_start_index)), "ACK")

Happy Coding!
:)
 
Share this answer
 

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