Trim Spaces from Beginning and end of Strings

Discussion in 'Questions (Windows Mobile)' started by BPak, Aug 29, 2007.

  1. BPak

    BPak Active Member Licensed User

    Is there a function to remove Spaces from the Beginning and end of a String?

    Looked through the help file in Strings and dont see any function for that.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    There is no built-in function but you have two options:
    1. If the string doesn't include spaces in the middle: StrReplace(string," ","")
    2. If the string may contain spaces you will need to go over each character and manually remove the leading / trailing spaces.
     
  3. BPak

    BPak Active Member Licensed User

    OK - Option 2 will have to be the one!

    Reading in a HTML file and stripping th markup and putting text into database - so need to strip of excess.

    Thanks you for your reply.
     
  4. maXim

    maXim Active Member Licensed User

    AllTrim, LTrim and RTrim functions

    I have resolved this way:

    Code:
    [COLOR=navy][B]Sub[/B][/COLOR] [COLOR=blue]fncALLTRIM[/COLOR][B][COLOR=navy]([/COLOR][/B]xString[COLOR=navy][B])[/B][/COLOR]
    [COLOR=darkgreen][B]   
    If[/B][/COLOR] [COLOR=sienna][B]StrLength[/B][/COLOR](xString) [COLOR=red][B]<[/B][/COLOR] 1 [COLOR=darkgreen][B]Then[/B][/COLOR] [B][COLOR=sienna]Return[/COLOR][/B] ""
        xs [B][COLOR=red]=[/COLOR][/B] [COLOR=blue]fncLTRIM[/COLOR](xString)
        xs [B][COLOR=red]=[/COLOR][/B] [COLOR=blue]fncRTRIM[/COLOR](xs)
        [COLOR=sienna][B]
    Return[/B][/COLOR] xs
    [COLOR=navy][B]
    End Sub[/B][/COLOR]
     
    [B][COLOR=
    #000080]Sub[/COLOR][/B] [COLOR=blue]fncLTRIM[/COLOR][B][COLOR=#000080]([/COLOR][/B]xString[B][COLOR=#000080])[/COLOR][/B]
        [B][COLOR=
    #006400]If[/COLOR][/B] [COLOR=sienna][B]StrLength[/B][/COLOR](xString) [COLOR=red][B]<[/B][/COLOR] 1 [COLOR=darkgreen][B]Then[/B][/COLOR] [B][COLOR=sienna]Return[/COLOR][/B] ""
        xs [B][COLOR=red]=[/COLOR][/B] xString
    [COLOR=purple]rLTRIM[/COLOR]:
        [B][COLOR=darkgreen]
    If[/COLOR][/B] [B][COLOR=sienna]StrLength[/COLOR][/B](xs) [B][COLOR=red]>[/COLOR][/B] 0 [COLOR=darkgreen][B]Then[/B][/COLOR]
             [COLOR=darkgreen][B]
    If[/B][/COLOR] [COLOR=sienna][B]SubString[/B][/COLOR](xs, 01) [B][COLOR=red]=[/COLOR][/B] [COLOR=sienna][B]Chr[/B][/COLOR](32) [B][COLOR=darkgreen]Then[/COLOR][/B]
                xs [B][COLOR=red]=[/COLOR][/B] [B][COLOR=sienna]SubString[/COLOR][/B](xs, 
    1, [COLOR=sienna][B]StrLength[/B][/COLOR](xs) [B][COLOR=red]-[/COLOR][/B] 1)
                [B][COLOR=sienna]Goto[/COLOR][/B] [COLOR=purple]rLTRIM[/COLOR]
             [COLOR=darkgreen][B]
    End If[/B][/COLOR]
        [B][COLOR=darkgreen]
    End If[/COLOR][/B]
        [B][COLOR=
    #a0522d]Return[/COLOR][/B] xs
    [B][COLOR=
    #000080]End Sub[/COLOR][/B]
     
    [B][COLOR=
    #000080]Sub[/COLOR][/B] [COLOR=blue]fncRTRIM[/COLOR][B][COLOR=#000080]([/COLOR][/B]xString[B][COLOR=#000080])[/COLOR][/B]
        [B][COLOR=
    #006400]If[/COLOR][/B] [COLOR=sienna][B]StrLength[/B][/COLOR](xString) [COLOR=red][B]<[/B][/COLOR] 1 [COLOR=darkgreen][B]Then[/B][/COLOR] [B][COLOR=sienna]Return[/COLOR][/B] ""
        xs [B][COLOR=red]=[/COLOR][/B] xString
    [COLOR=purple]rRTRIM[/COLOR]:
        [B][COLOR=darkgreen]
    If[/COLOR][/B] [B][COLOR=sienna]StrLength[/COLOR][/B](xs) [B]>[/B] 0 Then
            [COLOR=darkgreen][B]
    If[/B][/COLOR] [B][COLOR=sienna]SubString[/COLOR][/B](xs, [COLOR=sienna][B]StrLength[/B][/COLOR](xs) [B][COLOR=red]-[/COLOR][/B] 11) [B][COLOR=red]=[/COLOR][/B] [B][COLOR=sienna]Chr[/COLOR][/B](32) [COLOR=sienna][B][COLOR=darkgreen]Then[/COLOR][/B]
    [/COLOR]          xs [B][COLOR=red]=[/COLOR][/B] [B][COLOR=sienna]SubString[/COLOR][/B](xs, 
    0, [COLOR=sienna][B]StrLength[/B][/COLOR](xs) [B][COLOR=red]-[/COLOR][/B] 1)
                [B][COLOR=sienna]Goto[/COLOR][/B] [COLOR=purple]rRTRIM[/COLOR]
            [COLOR=darkgreen][B]
    End If[/B][/COLOR]
        [COLOR=sienna][B][COLOR=darkgreen]
    End If[/COLOR][/B]
    [/COLOR][B][COLOR=
    #a0522d]   Return[/COLOR][/B] xs
    [B][COLOR=
    #000080]End Sub[/COLOR][/B]
    this is not the best solution but works! ;)

    In my projects, exposed in "Italian Forum", you will find other examples and functions...

    Regards.
     
    Last edited: Aug 31, 2007
  5. specci48

    specci48 Well-Known Member Licensed User

    Hi maXim,

    just as a small hint, there is no need to code the ugly (;)) keyword Goto.
    The Do While function in connection with the Exit statement is a much better solution:

    Code:
    Sub fncLTRIM(xString)
        
    If StrLength(xString) < 1 Then Return ""
        xs = xString
        
    Do While StrLength(xs) > 0
            
    If SubString(xs, 01) = Chr(32Then
                xs = SubString(xs, 
    1, StrLength(xs) - 1)
            
    Else
           
    Exit
            
    End If
        
    Loop
        
    Return xs
    End Sub

    specci48
     
  6. maXim

    maXim Active Member Licensed User

    Hi specci48,

    sorry for my English :sign0013: but not for my example...

    Your observation is correct but the cycle DO WHILE ... LOOP results to be slower than around 0,25uS in comparison to the simple GOTO! (verified on my PPC: Acer n30). I don't believe that this is a problem HW since all the compilers suffer of it...

    Regards.
     
  7. specci48

    specci48 Well-Known Member Licensed User

    Hi maXim,

    it's OK! I never thought that your focus on speed is so high. :sign0188:
    And now we all know, that the compiler could be optimized on this sample code...


    specci48
     
  8. dzt

    dzt Active Member Licensed User

    Ciao Massimo,

    I was waiting for this answer as I was watching your posts, and you didn't disappoint me.
    :sign0188:
     
    Last edited: Aug 30, 2007
  9. BPak

    BPak Active Member Licensed User

    Great solutions for trimming the excess spaces from begging and trailing.

    Thank you for sharing them.

    I had a thought about just passing the string using StrAt to find the first and last Character that was not a space and then use SubString on those two numbers?

    "...Clarendon Red...."
    The full points at begin and end of above represent spaces (. to make visible for example).
    StrAt shows the first 3 as spaces and the 4th one as a non Space and sets the start location at 3 - the StrAt func then finds the last NON space to be at position 15 - SubString "...Clarendon Red....", FirstPos, SecondPos-FirstPos)

    Would that technique be a fast operation with the processor?
     
  10. tvrman

    tvrman Member Licensed User

    Oeps i did not read this post very well, im also busy to create
    an script for removing the html tags.
    let me know if you got one.

    thanks
     
    Last edited: Aug 31, 2007
  11. BPak

    BPak Active Member Licensed User

    tvrman - here is the code I use for stripping the HTML Markup.

    Pass the sub a Line of HTML

    Code:
    Sub DecodeTxt(TheTxt)
       
        tmp = 
    ""
        ch=
    ""   
        leng = StrLength(TheTxt)
        AON = 
    false
        
    For i = 0 To leng-1
            ch = StrAt(TheTxt, i)
            
    If (AON = falseThen
                
    If (ch = "<"Then
                    AON = 
    true
                
    Else
                    
    ' insert the char into text to save
                    tmp = tmp & ch
                
    End If
            
    Else
                
    If (ch = ">"Then 
           AON = 
    false
                
    End If
            
    End If ' else if AON is false (true) 
        Next
    '   TextConvert = LTRIM$(RTRIM$(tmp))   
        Return tmp
    End Sub
    I have taken this from C++ which I have been using for quite a while, and converted it to B4PPC use.
     
  12. Rioven

    Rioven Active Member Licensed User

    Hi, I did like this...
    s value should not be blank
    PHP:
    s=" LTRIM RTRIM  "
    'LTRIM
    lt=StrRemove (s, 0, StrIndexOf  (s, StrAt(StrReplace(s," ",""),0),0))

    '
    RTRIM
    s
    =lt
    rl
    =StrLength(s)
    i=rl
    Do While StrAt(s,i-1)=" "
    i=i-1
    If StrAt(s,i-1)<>" " Then Exit
    Loop 
    rt
    =StrRemove(s,i,rl-i)
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    After reading this thread I understand that it is a good time to build a Regular Expressions library ;)
     
  14. maXim

    maXim Active Member Licensed User

    Hi Erel,

    I approve Your decision! :sign0162:

    For when the new Library?

    Regards.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    I hope that it will be released in a week or two.
     
  16. maXim

    maXim Active Member Licensed User

    Hi Erel,

    Thanks for Your Job! :sign0188:

    I don't know well unfortunately the english, but If You need a beta-tester i’m ready for You! :)

    Regards.

    Massimo
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Thanks. And I don't worry about your English as RegEx works on any given string ;)
     
  18. tvrman

    tvrman Member Licensed User



    Thanks!!! Bpak:sign0188:
    It works very good.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Using the new Regex library:
    Code:
    [FONT=Courier New][SIZE=2][COLOR=#008000]'Regex1 is a Regex object and Match1 is a Match object.
    [/COLOR][/SIZE][/
    FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]Globals
    [/SIZE][/
    FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]End Sub
     
    [/COLOR][/SIZE][/
    FONT][FONT=Courier New][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]App_Start
     Match1.New1
     Regex1.New1([/SIZE][/
    FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"(?<=^\s*)\w.*\w|\w(?>=\s*)"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])
    [/SIZE][/
    FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff] Msgbox[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2](Trim([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]" some string with spaces "[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]))
    [/SIZE][/
    FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]End Sub
    [/COLOR][/SIZE][/
    FONT][FONT=Courier New][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff
    Sub [/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]Trim(s)
     Match1.Value = Regex1.Match(s)
    [/SIZE][/
    FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff] Return[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] Match1.String
    [/SIZE][/
    FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]End Sub
    [/COLOR][/SIZE][/
    FONT]
     
  20. abastien

    abastien Member Licensed User

    Trim function

    Code:
    Sub trim(txt)

       ch = SubString(txt, 
    01)
       
       
    Do While Asc(ch) < 33
          txt = SubString(txt, 
    1, StrLength(txt)-1)
          ch = SubString(txt, 
    01)
       
    Loop
       
       ch = SubString(txt, StrLength(txt)-
    11)
       
       
    Do While Asc(ch) < 33
          txt = SubString(txt, 
    0, StrLength(txt)-1)
          ch = SubString(txt, StrLength(txt)-
    11)
       
    Loop
       
       
    Return txt
       
    End Sub
     
Loading...