Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

Trim Spaces from Beginning and end of Strings

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-29-2007, 07:12 AM
Senior Member
 
Join Date: Apr 2007
Location: Qld, Australia
Posts: 107
Default Trim Spaces from Beginning and end of Strings

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.
Reply With Quote
  #2 (permalink)  
Old 08-29-2007, 10:31 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,734
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

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.
Reply With Quote
  #3 (permalink)  
Old 08-29-2007, 09:20 PM
Senior Member
 
Join Date: Apr 2007
Location: Qld, Australia
Posts: 107
Default

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.
Reply With Quote
  #4 (permalink)  
Old 08-30-2007, 08:08 AM
maXim's Avatar
Basic4ppc Veteran
 
Join Date: May 2007
Location: Sesto Fiorentino - Florence (Italy)
Posts: 224
Send a message via MSN to maXim Send a message via Skype™ to maXim
Awards Showcase
Beta Tester 
Total Awards: 1
Post AllTrim, LTrim and RTrim functions

I have resolved this way:

Code:
<font color="navy"><b>Sub</b></font> <font color="blue">fncALLTRIM</font><b><font color="navy">(</font></b>xString<font color="navy"><b>)</b></font>
<font color=
"darkgreen"><b>   If</b></font> <font color="sienna"><b>StrLength</b></font>(xString) <font color="red"><b><</b></font> 1 <font color="darkgreen"><b>Then</b></font> <b><font color="sienna">Return</font></b> ""
    xs <b><font color=
"red">=</font></b> <font color="blue">fncLTRIM</font>(xString)
    xs <b><font color=
"red">=</font></b> <font color="blue">fncRTRIM</font>(xs)
    <font color=
"sienna"><b>Return</b></font> xs
<font color=
"navy"><b>End Sub</b></font>
 
<b><font color=
"#000080">Sub</font></b> <font color="blue">fncLTRIM</font><b><font color="#000080">(</font></b>xString<b><font color="#000080">)</font></b>
    <b><font color=
"#006400">If</font></b> <font color="sienna"><b>StrLength</b></font>(xString) <font color="red"><b><</b></font> 1 <font color="darkgreen"><b>Then</b></font> <b><font color="sienna">Return</font></b> ""
    xs <b><font color=
"red">=</font></b> xString
<font color=
"purple">rLTRIM</font>:
    <b><font color=
"darkgreen">If</font></b> <b><font color="sienna">StrLength</font></b>(xs) <b><font color="red">></font></b> 0 <font color="darkgreen"><b>Then</b></font>
         <font color=
"darkgreen"><b>If</b></font> <font color="sienna"><b>SubString</b></font>(xs, 01) <b><font color="red">=</font></b> <font color="sienna"><b>Chr</b></font>(32) <b><font color="darkgreen">Then</font></b>
            xs <b><font color=
"red">=</font></b> <b><font color="sienna">SubString</font></b>(xs, 1, <font color="sienna"><b>StrLength</b></font>(xs) <b><font color="red">-</font></b> 1)
            <b><font color=
"sienna">Goto</font></b> <font color="purple">rLTRIM</font>
         <font color=
"darkgreen"><b>End If</b></font>
    <b><font color=
"darkgreen">End If</font></b>
    <b><font color=
"#a0522d">Return</font></b> xs
<b><font color=
"#000080">End Sub</font></b>
 
<b><font color=
"#000080">Sub</font></b> <font color="blue">fncRTRIM</font><b><font color="#000080">(</font></b>xString<b><font color="#000080">)</font></b>
    <b><font color=
"#006400">If</font></b> <font color="sienna"><b>StrLength</b></font>(xString) <font color="red"><b><</b></font> 1 <font color="darkgreen"><b>Then</b></font> <b><font color="sienna">Return</font></b> ""
    xs <b><font color=
"red">=</font></b> xString
<font color=
"purple">rRTRIM</font>:
    <b><font color=
"darkgreen">If</font></b> <b><font color="sienna">StrLength</font></b>(xs) <b>></b> 0 Then
        <font color=
"darkgreen"><b>If</b></font> <b><font color="sienna">SubString</font></b>(xs, <font color="sienna"><b>StrLength</b></font>(xs) <b><font color="red">-</font></b> 11) <b><font color="red">=</font></b> <b><font color="sienna">Chr</font></b>(32) <font color="sienna"><b><font color="darkgreen">Then</font></b>
</font>          xs <b><font color=
"red">=</font></b> <b><font color="sienna">SubString</font></b>(xs, 0, <font color="sienna"><b>StrLength</b></font>(xs) <b><font color="red">-</font></b> 1)
            <b><font color=
"sienna">Goto</font></b> <font color="purple">rRTRIM</font>
        <font color=
"darkgreen"><b>End If</b></font>
    <font color=
"sienna"><b><font color="darkgreen">End If</font></b>
</font><b><font color=
"#a0522d">   Return</font></b> xs
<b><font color=
"#000080">End Sub</font></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 by maXim : 08-31-2007 at 09:09 AM.
Reply With Quote
  #5 (permalink)  
Old 08-30-2007, 02:52 PM
specci48's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: Germany
Posts: 1,057
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

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
Reply With Quote
  #6 (permalink)  
Old 08-30-2007, 06:33 PM
maXim's Avatar
Basic4ppc Veteran
 
Join Date: May 2007
Location: Sesto Fiorentino - Florence (Italy)
Posts: 224
Send a message via MSN to maXim Send a message via Skype™ to maXim
Awards Showcase
Beta Tester 
Total Awards: 1
Post

Hi specci48,

sorry for my English 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.
Reply With Quote
  #7 (permalink)  
Old 08-30-2007, 07:28 PM
specci48's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: Germany
Posts: 1,057
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Hi maXim,

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


specci48
Reply With Quote
  #8 (permalink)  
Old 08-30-2007, 07:36 PM
dzt's Avatar
dzt dzt is offline
Basic4ppc Veteran
 
Join Date: May 2007
Location: Ioannina, Greece
Posts: 356
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Ciao Massimo,

I was waiting for this answer as I was watching your posts, and you didn't disappoint me.
__________________
Dimitris Zacharakis
http://www.terracom.gr

Last edited by dzt : 08-30-2007 at 07:38 PM.
Reply With Quote
  #9 (permalink)  
Old 08-30-2007, 09:40 PM
Senior Member
 
Join Date: Apr 2007
Location: Qld, Australia
Posts: 107
Default

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?
Reply With Quote
  #10 (permalink)  
Old 08-31-2007, 02:51 PM
Knows the basics
 
Join Date: May 2007
Posts: 63
Default

Quote:
Originally Posted by BPak View Post
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 by tvrman : 08-31-2007 at 02:56 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling Problems - Images must not include spaces J12345T Bug Reports 8 06-01-2008 07:55 AM
esiste la funzione trim? marcomarco Italian Forum 2 04-16-2008 08:03 AM
SORT / TRIM Table(CSV) sintaq Questions (Windows Mobile) 1 03-24-2008 09:52 AM
Remove spaces from string forisco Questions (Windows Mobile) 2 11-16-2007 10:07 AM
Externalization of strings for localisation Tirs Questions (Windows Mobile) 2 05-29-2007 10:19 PM


All times are GMT. The time now is 11:06 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0