locked
Replacing parts of a string in small basic? RRS feed

  • Question


  • Not sure if this is possible in SB since it is such a high level programming language. I was just wondering if you can replace certain SubText within a string with a different string. For example:

    The string is: Hell\e

    The \e indicates that I want to replace it with è.

    Any help is much appreciated.

    Kind regards,

    Ben


    • Edited by benboy847 Sunday, March 9, 2014 5:18 PM
    Tuesday, May 21, 2013 8:33 PM

Answers

  • A parser is simply a function that performs operations on text (such as the engine that provides syntax highlighting in the Small Basic IDE.)

    Here is a simple parser that fits your purposes:

    TextToReplace = "Hell\e" 'this is the text we will parse
    TextWindow.WriteLine("Text to replace is " + TextToReplace)
    
    For i = 1 To Text.GetLength(TextToReplace) 'iterate through each character of string
      If Text.GetSubText(TextToReplace, i, 1) = "\"  Then 'if the character is a \
        If Text.GetSubText(TextToReplace, i + 1, 1) = "e" Then 'if the character after it is an e
          NewText = ""
          For x = 1 To i - 1 'iterate through all characters before the \
            PreText = PreText + Text.GetSubText(TextToReplace, x, 1) 'add that character to our new text
          EndFor
          PreText = PreText + "è" 'perform the actual replacement
          For x = i + 2 To  Text.GetLength(TextToReplace) 'go through and add rest of text
            PreText  = PreText + Text.GetSubText(TextToReplace, x ,1) 'append text
          EndFor
          TextToReplace = PreText
        EndIf
      EndIf  
    EndFor
    
    TextWindow.WriteLine("Parsed text is " + TextToReplace)

    Note that this will only work for replacing one instance of "\e". You can extend it if you would like to be able to replace multiple instances of "\e" or to replace other strings as well.

    Also note that there are operations in the "Text" class that could eliminate some of the for loops but I personally prefer to break string into character arrays for processing.


    Please mark any answers and "vote as helpful" any posts that help you!


    Tuesday, May 21, 2013 11:05 PM
    Answerer

All replies

  • There's no native capability for replacing a substring for another 1 within a string!

    You have to write your own parser to scan into it. It's tricky, but we can help you make 1 if you try!  :D


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Tuesday, May 21, 2013 9:40 PM
    Answerer
  • parser???
    Tuesday, May 21, 2013 9:41 PM
  • A parser is simply a function that performs operations on text (such as the engine that provides syntax highlighting in the Small Basic IDE.)

    Here is a simple parser that fits your purposes:

    TextToReplace = "Hell\e" 'this is the text we will parse
    TextWindow.WriteLine("Text to replace is " + TextToReplace)
    
    For i = 1 To Text.GetLength(TextToReplace) 'iterate through each character of string
      If Text.GetSubText(TextToReplace, i, 1) = "\"  Then 'if the character is a \
        If Text.GetSubText(TextToReplace, i + 1, 1) = "e" Then 'if the character after it is an e
          NewText = ""
          For x = 1 To i - 1 'iterate through all characters before the \
            PreText = PreText + Text.GetSubText(TextToReplace, x, 1) 'add that character to our new text
          EndFor
          PreText = PreText + "è" 'perform the actual replacement
          For x = i + 2 To  Text.GetLength(TextToReplace) 'go through and add rest of text
            PreText  = PreText + Text.GetSubText(TextToReplace, x ,1) 'append text
          EndFor
          TextToReplace = PreText
        EndIf
      EndIf  
    EndFor
    
    TextWindow.WriteLine("Parsed text is " + TextToReplace)

    Note that this will only work for replacing one instance of "\e". You can extend it if you would like to be able to replace multiple instances of "\e" or to replace other strings as well.

    Also note that there are operations in the "Text" class that could eliminate some of the for loops but I personally prefer to break string into character arrays for processing.


    Please mark any answers and "vote as helpful" any posts that help you!


    Tuesday, May 21, 2013 11:05 PM
    Answerer
  • Hi Ben

    Yes sb can do this with the Text object.

    oldString =  "Hell\e"
    charCode = 233 'code for accented e.
    
    If Text.IsSubText(oldString, "\") = "true" Then
      index = Text.GetIndexOf(oldString, "\")  
    EndIf
    
    firstPart = Text.GetSubText(oldString, 1, index-1)  
    newString = Text.Append(firstPart, Text.GetCharacter(charCode))
    
    TextWindow.WriteLine(newString)




    You have to have a dream so you can get up in the morning. - Billy Wilder



    Tuesday, May 21, 2013 11:12 PM
  • I love this forum! Within 4 hours three people jump in to help out!!!

    Ed Price (a.k.a User Ed), SQL Server Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    Wednesday, May 22, 2013 1:49 AM
  • Here you go, This works pretty well:

    Text = "dheyshshell/efuydfjrghell/e"
    TextUc = Text.ConvertToUpperCase(Text)
    For i = 1 To Text.GetLength(Text)
      If Text.GetSubText(TextUc,i,1) = "/" And Text.GetSubText(TextUc,i+1,1) = "E" Then
        NewText = NewText + "è"
      ElseIf Text.GetSubText(TextUc,i - 1,1) <> "/" And Text.GetSubText(TextUc,i,1) <> "E" Then
        NewText = NewText + Text.GetSubText(TextUc,i,1)
      EndIf
    EndFor
      TextWindow.WriteLine(NewText))


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11


    Wednesday, May 22, 2013 2:04 AM
    Answerer
  • There is an Text.Replace Extension. 

    http://extext4smallbasic.codeplex.com/

    • Text.Split( text, separator )
    • Text.Replace( text, oldtext, newtext )
    • Text.Left( text, length )
    • Text.Right( text, length )
    • Text.Mid( text, start, length )
    • Text.Len( text )
    Wednesday, May 22, 2013 2:29 AM
    Answerer
  • @Jibba Jabba:

    Your program doesn't work if the searchstring is not present in oldString, and also id the searchstring is not the last character the part after the searchstring is ignored. 

    @Zock:

    Why do you convert to uppercase?


    Jan [ WhTurner ] The Netherlands

    Wednesday, May 22, 2013 10:35 AM
    Answerer
  • Thanks for checking Jan. I wasn't really trying to do the whole solution for Ben or return output if it didn't contain the criteria, just get the ball rolling for him.

    I think this picks up everything for user input assuming the criteria is not a variable beyond "\e" and "\E". What do you think?

    oldString =  "Hell\e.,njhk,\ekll\Ekj\b\e/e/e\E.kj\ehj"
    TextWindow.WriteLine(oldString)
    subLowCase = Text.GetCharacter(233) 'code for accented e.
    subUpperCase = Text.GetCharacter(201) 'code for accented E.
    
    If Text.IsSubText(oldString, "\e") Or Text.IsSubText(oldString, "\E") Then
      For i = 1 To Text.GetLength(oldString)
        If Text.GetSubText(oldString, i, 2) = "\e" Then     
          subTxt = Text.Append(Text.GetSubText(oldString, 1, i-1), subLowCase)    
          oldString = Text.Append(subTxt, Text.GetSubTextToEnd(oldString, i+2))  
        EndIf
      EndFor
      For i = 1 To Text.GetLength(oldString)
        If Text.GetSubText(oldString, i, 2) = "\E" Then     
          subTxt = Text.Append(Text.GetSubText(oldString, 1, i-1), subUpperCase)    
          oldString = Text.Append(subTxt, Text.GetSubTextToEnd(oldString, i+2))  
        EndIf
      EndFor
    EndIf
    
    parsedString = oldString
    TextWindow.WriteLine(parsedString)



    You have to have a dream so you can get up in the morning. - Billy Wilder


    • Edited by Jibba j Wednesday, May 22, 2013 2:26 PM
    Wednesday, May 22, 2013 1:51 PM
  • Yes, works as required. If there are more replacements, you can probably use two nested loops, and put the text to search for and the replacement text each in an array (I have not tried this) 

    Jan [ WhTurner ] The Netherlands

    Wednesday, May 22, 2013 2:30 PM
    Answerer
  • thanks. Good practice for me. :)

    You have to have a dream so you can get up in the morning. - Billy Wilder

    Wednesday, May 22, 2013 2:36 PM
  • I have adapted the program as suggested in my last post. It works now for 4 replacements and can easy be expanded to more.

    oldString =  "Hell\e.,njhk,\Akll\Ekj\b\e/e/e\E.kj\ehj\ae"
    TextWindow.WriteLine(oldString)
    string1="eEaA"  'escaped chars
    string2="éÉáÁ"  'replacement chars
    ls1=text.GetLength(string1)
    ls2=text.GetLength(string2)
    If ls1<>ls2 Then
      TextWindow.WriteLine("string 1 and 2 don't have the same length")
      TextWindow.Pause()
      Program.End()
    else
      For i=1 To ls1
        s1[i]=text.GetSubText(string1,i,1)
        s2[i]=text.GetSubText(string2,i,1)
      EndFor
      If Text.IsSubText(oldString, "\")  Then
        For i = 1 To Text.GetLength(oldString)
          If Text.GetSubText(oldString, i, 1) = "\" Then     
            code=Text.GetSubText(oldString,i+1,1)
            For j=1 To ls1
              If code=s1[j] then
                subTxt = Text.Append(Text.GetSubText(oldString, 1, i-1), s2[j])    
                oldString = Text.Append(subTxt, Text.GetSubTextToEnd(oldString, i+2))  
              EndIf
            endfor
          endif
        EndFor
      EndIf
    endif
    parsedString = oldString
    TextWindow.WriteLine(parsedString)
    


    Jan [ WhTurner ] The Netherlands

    Wednesday, May 22, 2013 3:12 PM
    Answerer
  • @Jibba Jabba:

    Your program doesn't work if the searchstring is not present in oldString, and also id the searchstring is not the last character the part after the searchstring is ignored. 

    @Zock:

    Why do you convert to uppercase?


    Jan [ WhTurner ] The Netherlands

    I convert to upper case so the input can be either e/ or E/. (Probably don't need to do this)


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11


    Wednesday, May 22, 2013 5:12 PM
    Answerer
  • This works for as many replacements as you put in:

    Text = "dheyshshell/efuydfjr/e/e/eghell/edhe/eyshshell/efuydfjrghell/e"
    TextUc = Text.ConvertToUpperCase(Text)
    For i = 1 To Text.GetLength(Text)
      If Text.GetSubText(TextUc,i,1) = "/" And Text.GetSubText(TextUc,i+1,1) = "E" Then
        NewText = NewText + "è"
      ElseIf Text.GetSubText(TextUc,i - 1,1) <> "/" And Text.GetSubText(TextUc,i,1) <> "E" Then
        NewText = NewText + Text.GetSubText(TextUc,i,1)
      EndIf
    EndFor
      TextWindow.WriteLine(NewText)


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Wednesday, May 22, 2013 5:16 PM
    Answerer
  • Jan, thanks for the sample code. :) and testing :)

    You have to have a dream so you can get up in the morning. - Billy Wilder

    Wednesday, May 22, 2013 9:49 PM