no space at start and end line

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

no space at start and end line

Post by jmburnod » Tue Feb 22, 2011 2:29 pm

Hi All,
I need to delete first char and last char of a string if it is a space

I'm a little confuse with this script:

Code: Select all

function NoSPaceDebEndLine pLeT
 repeat until char 1 of pLeT <> " " --•• work
      delete char 1 of pLeT
      wait 1 milliseconds
   end repeat
   
   repeat until char-1 of pLeT <> " " --•• dont work
      delete char-1 of pLeT
      wait 1 milliseconds
   end repeat
   return pLeT
end NoSPaceDebEndLine
I dont understand why the second repeat dont work

An other way tested is

Code: Select all

function NoSPaceDebEndLine pLeT
   put the num of words of pLeT into nbw
   return word 1 to nbw of pLeT
end NoSPaceDebEndLine
https://alternatic.ch

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: no space at start and end line

Post by Dixie » Tue Feb 22, 2011 3:01 pm

hi...

Would this work for you ?

Code: Select all

on mouseUp
   put line 1 of fld 1 into myVar
   put stripSpace (myVar) into fld 2
end mouseUp

function stripSpace theString
   repeat forever
      if char 1 of theString = space then
         delete char 1 of theString
      else
         exit repeat
      end if
   end repeat
   
   repeat forever
      if the last char of theString = space then
         delete the last char of theString
      else
         exit repeat
      end if
   end repeat
   
   return theString
end stripSpace
be well

Dixie

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: no space at start and end line

Post by jmburnod » Tue Feb 22, 2011 3:17 pm

Hi,
No,
The second repeat dont work. WHY ?

All the best

Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: no space at start and end line

Post by dunbarx » Tue Feb 22, 2011 4:23 pm

Everybody's offerings seem to be sound, and they all work for me.

I tried them all on an string like: " aa bb cc " (one space before and two spaces after the text)

What are you getting as output?

Craig Newman

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: no space at start and end line

Post by jmburnod » Tue Feb 22, 2011 5:00 pm

Hi Craig,
Yes it work with " aa bb cc "
The problem was in my string : The last char was a chartonum 13
Sorry for that


Best

Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: no space at start and end line

Post by dunbarx » Tue Feb 22, 2011 5:35 pm

So there are two ways to do this:

1- Work through the string, eliminating spaces at the beginning and spaces and returns at the end. This is easy and straightforward.

2- Let the chunking power of LC do all the work for you. This is a sneak attack, and actually much more elegant...

function stripSpacesAndReturns var
return word 1 to -1 of var
end stripSpacesAndReturns

Try it.

Craig Newman

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: no space at start and end line

Post by Dixie » Tue Feb 22, 2011 5:50 pm

Craig...

Elegant indeed ! :D

go steady

Dixie

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: no space at start and end line

Post by jmburnod » Tue Feb 22, 2011 5:55 pm

Yes,

I tried this (the second script of my first message about this topic) but i thougt it was not the best way
Thank one more

Jean-Marc
https://alternatic.ch

Post Reply