Android Code Snippet String replace

Discussion in 'Code Snippets' started by MitchBu, Apr 15, 2014.

  1. MitchBu

    MitchBu Active Member Licensed User

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

    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.

    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 & _
    Return chain
    End Sub
    Dependencies: This sub uses core commands, and does not require any additional library.

    Tags: string.replace Replace() strings
    Last edited: Apr 16, 2014
    DonManfred likes this.
  2. DonManfred

    DonManfred Expert Licensed User

  3. MitchBu

    MitchBu Active Member Licensed User

  4. DonManfred

    DonManfred Expert Licensed User

  5. LucaMs

    LucaMs Expert Licensed User

    Thanks for your snippet, MitchBu.

    You should change:
    If chain.IndexOf(search) = 0 Then Return
    If chain.IndexOf(search) = 0 Then Return Chain
  6. MitchBu

    MitchBu Active Member Licensed User

    Right. If the sub does nothing it should return the same string. Thank you. Fixed :)
  7. JOTHA

    JOTHA Well-Known Member Licensed 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:

    Dim s As String
    For i = 1 To 10
    s = 
    "x"&s&", x"& i &"x "&s.SubString(s.Length)&""
    s = s.SubString(
    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?
  8. MitchBu

    MitchBu Active Member Licensed User

    You don't need Substitute to directly obtain what you want.

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

    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 & ","
    JOTHA likes this.
  9. JOTHA

    JOTHA Well-Known Member Licensed 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
    For nn = 1 To 1440
    ... so I have to write
    Thank you anyway!
    Happy EASTER!
  10. stevel05

    stevel05 Expert Licensed 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:

    Dim Arr(1441As String
    For i = 0 To 1440
            Arr(i) = i
    'Check it is a string array
    For i = 0 To 10
    Dim S As String = Arr(i)
    Last edited: Apr 14, 2017
  11. JOTHA

    JOTHA Well-Known Member Licensed 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
  12. XbNnX_507

    XbNnX_507 Active Member Licensed User

    Regex.Replace("x", s, """")
    Star-Dust likes this.
  13. MitchBu

    MitchBu Active Member Licensed User

    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.
  14. NeoTechni

    NeoTechni Well-Known Member Licensed 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.
    'Case insensitive replace
    public Sub Replace(Text As String, Search As String, ReplaceWith As StringAs 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)
    Return Text
    End Sub
    Last edited: Nov 3, 2018
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice