Android Code Snippet String replace

SubName: substitute (chain AsString, search AsString, replaced AsString)

Description:
This is meant to replace a sequence of characters by another within a string.
It uses a syntax close to VB6 REPLACE()
- Chain is the string where characters will be searched for
- search is the string to look for
- Replaced is the string that will replace search

This sub supports the chr(0) character which string.Replace does not.

B4X:
Sub substitute(chain As String, search As String, replaced As String)
If chain.IndexOf(search) = 0 Then Return chain
Dim gauche, droite As String
Do While chain.IndexOf(search)>0
chain = chain.substring2(0,chain.IndexOf(search)) & replaced & _
chain.SubString(chain.IndexOf(search)+search.Length)
Loop
Return chain
End Sub

Dependencies: This sub uses core commands, and does not require any additional library.

Tags: string.replace Replace() strings
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello MitchBu,

I need a String like this one: "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ...
and I can generate a String as follows:

B4X:
Dim s As String
For i = 1 To 10
s = "x"&s&", x"& i &"x "&s.SubString(s.Length)&""
Next
s = s.SubString(12)
Log(s)

The result is: x1x, x2x, x3x, x4x, x5x, x6x, x7x, x8x, x9x, x10x

With your code snippet it is not possible to search the "x" and replace them with " so that the result would be

--> "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" <---

How can I do that?
 

MitchBu

Well-Known Member
Licensed User
Longtime User
You don't need Substitute to directly obtain what you want.

Just create a variable for the quote. I use "q":

B4X:
Dim q As String = Chr(34)
Dim s As String
For nn = 1 To 12
s = s & q & nn & q
If nn <12 Then s = s & ","
Next
Log(s)
 

JOTHA

Well-Known Member
Licensed User
Longtime User
MitchBu, thank you very much for your help!

The solution is to
Dim q AsString = Chr(34)
... i made several experiments with " and used it to try as char, but there was no way. I didn't know that is is possible to use Chr(34) in the code.

Your code was exactly what I asked for, but it doesn't really help me, because I want to use it in an
Array As String("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")
an this doesn't work. I could write it hardcoded, but in the App I need it for
B4X:
For nn = 1 To 1440
... so I have to write
Array As String("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", .................................. "1440")

Thank you anyway!
Happy EASTER!
 

stevel05

Expert
Licensed User
Longtime User
You don't need to create a string array that way, B4x will do casting for you. This will give you a string array:

B4X:
    Dim Arr(1441) As String
    For i = 0 To 1440
        Arr(i) = i
    Next
  
'Check it is a string array
    Log(GetType(Arr(3)))
  
    For i = 0 To 10
        Dim S As String = Arr(i)
        Log(S)
    Next
 
Last edited:

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello @stevel05,

thank you for your answer, but the result of Log(s) is "10". That is not what I was searching for.

But it doesnt matter, because I dont need it anymore ... thanks
 

XbNnX_507

Active Member
Licensed User
Longtime User
I need a String like this one: "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ...
and I can generate a String as follows:

B4X:
Dim s As String
For i = 1 To 10
s = "x"&s&", x"& i &"x "&s.SubString(s.Length)&""
Next
s = s.SubString(12)
Log(s)

The result is: x1x, x2x, x3x, x4x, x5x, x6x, x7x, x8x, x9x, x10x

--> "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" <---

How can I do that?

B4X:
 Regex.Replace("x", s, """")
 

MitchBu

Well-Known Member
Licensed User
Longtime User
MitchBu, thank you very much for your help!

The solution is to ... i made several experiments with " and used it to try as char, but there was no way. I didn't know that is is possible to use Chr(34) in the code.

Your code was exactly what I asked for, but it doesn't really help me, because I want to use it in an an this doesn't work. I could write it hardcoded, but in the App I need it for
B4X:
For nn = 1 To 1440
... so I have to write

Thank you anyway!
Happy EASTER!

You know, when posting a question, if you hold what you want to achieve, you get partial or inadequate replies, both from me, or the excellent Regex suggestion from XbNnX_507.

If you need that in code, direct the output to log(), run the program, and simply copy it to paste in your source.

You could also use a list, and append the numbers, then convert into an array.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
You should actually be checking if the value is > -1, not > 0.

I made a version of your code that is case insensitive, in case anyone needs it.
B4X:
'Case insensitive replace
public Sub Replace(Text As String, Search As String, ReplaceWith As String) As String
  Search = Search.ToLowerCase
   Dim Lower As String = Text.ToLowerCase, Index As Int = Lower.IndexOf(Search)
   Do While Index>-1
       Text = Text.substring2(0,Index) & ReplaceWith & Text.SubString(Index+Search.Length)
       Lower = Lower.substring2(0,Index) & ReplaceWith & Lower.SubString(Index+Search.Length)
       Index = Lower.IndexOf(Search)
   Loop
   Return Text
End Sub
 
Last edited:
Top