Help on String

cnicolapc

Active Member
Licensed User
Longtime User
hi,
how can I replace a word in a string?
eg:
If i use speech to text and I say "3381554343" and return 33cotto1554343
I would like to find and replace "cotto" with "8", it is fair use.
.Contains e .Replace and is the correct way?
Thank You
Nicola
 

cnicolapc

Active Member
Licensed User
Longtime User
hi,
Thanks for the answer, but in the previous post I forgot to mention that I also want to find a word to be able to separate from the string.
eg:
by the string "I called the number 33cotto1224567 but is busy"
I would like to extract only "33cotto1224567".
I would say the software if the string contains "eight" or "cotto" to extract the number "33cotto1224567" to be able to develop and reassign the form "3381224567".
The argument is a little convoluted, I hope you have understood.
thanks

Thanks for the help
 
Upvote 0

admac231

Active Member
Licensed User
Longtime User
Oh right. Well there is a few ways to accomplish this, all dependant on how much you know about the string (i.e if it is static then it would be very easy but dynamic is harder)

So lets try with static (that is, the string is always "I called the number 33cotto1224567 but is busy").

Look for the start and end of the part you want to extract:
B4X:
start = string.IndexOf("number ")
end = string.IndexOf(" but")

Now cut the string down to only include those characters:
B4X:
string = string.SubString2(start+7,end) //+7 because "number " is 7 characters

This leaves us with string = "33cotto1224567" so now we just search for "eight" or "cotto" and change if needs be:
B4X:
If string.IndexOf("eight") > -1 Then
     string = string.replace("eight","8")
Else If string.IndexOf("cotto") > -1 Then
     string = string.replace("cotto","8")
End If

And string now = "3381224567".

All the code:
B4X:
Dim string As String
Dim start As Integer
Dim end As Integer

string = "I called the number 33cotto1224567 but is busy"

start = string.IndexOf("number ")
end = string.IndexOf(" but")

string = string.SubString2(start+7,end)

If string.IndexOf("eight") > -1 Then
     string = string.replace("eight","8")
Else If string.IndexOf("cotto") > -1 Then
     string = string.replace("cotto","8")
End If

Log("Number: " & string)
 
Last edited:
Upvote 0

cnicolapc

Active Member
Licensed User
Longtime User
thank you very much,
In fact, I was thinking ... a dynamic string generated by speech recognition, although I think it's a difficult thing.
Nicola
 
Upvote 0
Top