Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed 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
Junior Member
 
Join Date: Apr 2007
Location: Qld, Australia
Posts: 42
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: 2,806
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
Junior Member
 
Join Date: Apr 2007
Location: Qld, Australia
Posts: 42
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
Senior Member
 
Join Date: May 2007
Location: Florence, Italy
Posts: 135
Send a message via MSN to maXim Send a message via Skype™ to maXim
Post AllTrim, LTrim and RTrim functions

I have resolved this way:

Code:
Sub fncALLTRIM(xString)
   If StrLength(xString) < 1 Then Return ""
    xs = fncLTRIM(xString)
    xs = fncRTRIM(xs)
    Return xs
End Sub
 
Sub fncLTRIM(xString)
    If StrLength(xString) < 1 Then Return ""
    xs = xString
rLTRIM:
    If StrLength(xs) > 0 Then
         If SubString(xs, 0, 1) = Chr(32) Then
            xs = SubString(xs, 1, StrLength(xs) - 1)
            Goto rLTRIM
         End If
    End If
    Return xs
End Sub
 
Sub fncRTRIM(xString)
    If StrLength(xString) < 1 Then Return ""
    xs = xString
rRTRIM:
    If StrLength(xs) > 0 Then
        If SubString(xs, StrLength(xs) - 1, 1) = Chr(32) Then
          xs = SubString(xs, 0, StrLength(xs) - 1)
            Goto rRTRIM
        End If
    End If
   Return xs
End Sub
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: 575
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, 0, 1) = Chr(32) Then
            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
Senior Member
 
Join Date: May 2007
Location: Florence, Italy
Posts: 135
Send a message via MSN to maXim Send a message via Skype™ to maXim
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: 575
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: Greece
Posts: 353
Awards Showcase
Forum Contributer 
Total Awards: 1
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
Junior Member
 
Join Date: Apr 2007
Location: Qld, Australia
Posts: 42
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: 60
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 On
Pingbacks are On
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 & Help Needed 1 03-24-2008 09:52 AM
Remove spaces from string forisco Questions & Help Needed 2 11-16-2007 10:07 AM
Externalization of strings for localisation Tirs Questions & Help Needed 2 05-29-2007 10:19 PM


All times are GMT. The time now is 05:30 AM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0