Trim Spaces from Beginning and end of Strings

BPak

Active Member
Licensed User
Longtime 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.
 

BPak

Active Member
Licensed User
Longtime 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.
 

maXim

Active Member
Licensed User
Longtime User
AllTrim, LTrim and RTrim functions

I have resolved this way:

B4X:
[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, 0, 1) [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] 1, 1) [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:

specci48

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

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


specci48
 

maXim

Active Member
Licensed User
Longtime 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.
 

specci48

Well-Known Member
Licensed User
Longtime 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
 

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:

BPak

Active Member
Licensed User
Longtime 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?
 

tvrman

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.

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:

BPak

Active Member
Licensed User
Longtime User
tvrman - here is the code I use for stripping the HTML Markup.

Pass the sub a Line of HTML

B4X:
Sub DecodeTxt(TheTxt)
   
    tmp = ""
    ch=""   
    leng = StrLength(TheTxt)
    AON = false
    For i = 0 To leng-1
        ch = StrAt(TheTxt, i)
        If (AON = false) Then
            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.
 

Rioven

Active Member
Licensed User
Longtime 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)
 

maXim

Active Member
Licensed User
Longtime User
Hi Erel,

I approve Your decision! :sign0162:

For when the new Library?

Regards.
 

maXim

Active Member
Licensed User
Longtime 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
 

tvrman

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

Pass the sub a Line of HTML

B4X:
Sub DecodeTxt(TheTxt)
   
    tmp = ""
    ch=""   
    leng = StrLength(TheTxt)
    AON = false
    For i = 0 To leng-1
        ch = StrAt(TheTxt, i)
        If (AON = false) Then
            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.



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

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using the new Regex library:
B4X:
[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]
 

abastien

Member
Licensed User
Trim function

B4X:
Sub trim(txt)

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