Finding the first space in a line of text

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Finding the first space in a line of text

Post by quailcreek » Tue Jan 12, 2016 12:38 am

Hi,
I'm reformatting some text and I need to replace the first space in each line of text with 2 tabs. This is part on the code I have. How do I tell LC that I'm referring to the first space in N?

Code: Select all

repeat for each line N in tTextStart
      replace the first space of N with tab & tab
      put N & cr after theConvertedText
   end repeat
Tom
MacBook Pro OS Mojave 10.14

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: Finding the first space in a line of text

Post by dave.kilroy » Tue Jan 12, 2016 2:28 am

Hi Tom - offset() finds the first instance of a char in a string and returns the char number, so the sample below returns the char number of the first space:

Code: Select all

on mouseUp
     put "ssdfsdo xxdio dkkd" into tText
     put offset(space,tText)
end mouseUp
"...this is not the code you are looking for..."

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Finding the first space in a line of text

Post by quailcreek » Tue Jan 12, 2016 2:39 am

Hi Dave,
Good to hear from you. Offset... brain freeze I did't think of that. Thanks a lot.
Tom
MacBook Pro OS Mojave 10.14

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Finding the first space in a line of text

Post by quailcreek » Wed Feb 17, 2016 10:14 pm

Well I thought I had this worked out... not! I'm trying to replace the first space in a string with 2 tabs. The problem is that the code I have is replacing all of the spaces in the string. What am I missing?

Code: Select all

on mouseUp
   put fld "Start" into tTextStart
   
   repeat for each line N in tTextStart
      put offset(space,N) into theSpace
      replace char theSpace of N with tab & tab in N
      put N & cr after theFinishedText
   end repeat
   put theFinishedText into fld "End"
end mouseUp
Tom
MacBook Pro OS Mojave 10.14

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Finding the first space in a line of text

Post by Klaus » Wed Feb 17, 2016 10:26 pm

Hi Tom,

"repeat for each ..." is READ ONLY!
So you cannot modify N (in your example) without unexspected results, if at all! 8)

Try this:

Code: Select all

on mouseUp
   put fld "Start" into tTextStart
   repeat for each line N in tTextStart
      put N into M
      put offset(space,M) into theSpace
      replace char theSpace of M with tab & tab in M
      put M & cr after theFinishedText
   end repeat
   put theFinishedText into fld "End"
end mouseUp
Best

Klaus

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Finding the first space in a line of text

Post by quailcreek » Wed Feb 17, 2016 10:38 pm

Hi Klaus,
I appreciate the help. Same results. It replaces all of the spaces.
Tom
MacBook Pro OS Mojave 10.14

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: Finding the first space in a line of text

Post by rkriesel » Wed Feb 17, 2016 11:32 pm

quailcreek wrote:... What am I missing?
The dictionary says
Use the replace command to replace all instances of one string with another string.
So you could just

Code: Select all

put tab & tab into char theSpace of N
-- Dick

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Finding the first space in a line of text

Post by quailcreek » Wed Feb 17, 2016 11:57 pm

That did it. Thanks Dick!
Tom
MacBook Pro OS Mojave 10.14

gagsoft
Posts: 168
Joined: Sat Jun 29, 2013 7:56 pm

Find And Replace spaces in line of text

Post by gagsoft » Fri Jun 14, 2019 8:28 pm

Hi All

I am trying to write script to find and replace the spaces in a line of text in a field called "text"
Managed to Concatenation the "FILE:///" and "/" to a file path that I want to convert into a hyperlink.
Now I want to find and replace the spaces between words with "%"
This is the result that I need:
file:////Volumes/MPB1Tb/eBooks/LG%Projector&Manual/LG&Projector%ENG.pdf/
This is my code so far:
on mouseUp
answer file "A text file"
if it <> "" then
-- put it into field "text"
put "file:////" & it & "/" into fld "text"
else
--no file was selected, or cancel was pressed
beep
end if
end mouseUp
Would appreciate any pointers
Thanks

Peter

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Finding the first space in a line of text

Post by bogs » Fri Jun 14, 2019 9:21 pm

Hehe, the answer to your question was literally 2 posts above your post - kinda - why not use 'replace' ?
Selection_001.png
Replace this with that...
Image

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Finding the first space in a line of text

Post by Simon Knight » Sat Jun 15, 2019 7:43 am

to a file path
You may have to deal with more than spaces, have you tried URLEncode ? The following may be of use as a starting point, note that %20 is a space character :

Code: Select all

function EncodeFilePath pFilePath
   put the URLEncode of pFilePath  into tURLEdncodeFilePath
   
   replace "+" with "%20" in tURLEdncodeFilePath  
   replace "|" with "%7C" in tURLEdncodeFilePath  
end encodeFilePath
best wishes
Skids

Post Reply

Return to “Talking LiveCode”