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: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

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

Finding the first space in a line of text

Post by quailcreek »

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
Contact:

Re: Finding the first space in a line of text

Post by dave.kilroy »

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

Re: Finding the first space in a line of text

Post by quailcreek »

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

Re: Finding the first space in a line of text

Post by quailcreek »

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: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Finding the first space in a line of text

Post by Klaus »

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

Re: Finding the first space in a line of text

Post by quailcreek »

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: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: Finding the first space in a line of text

Post by rkriesel »

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

Re: Finding the first space in a line of text

Post by quailcreek »

That did it. Thanks Dick!
Tom
MacBook Pro OS Mojave 10.14
PeterG
Posts: 168
Joined: Sat Jun 29, 2013 7:56 pm

Find And Replace spaces in line of text

Post by PeterG »

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: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Finding the first space in a line of text

Post by bogs »

Hehe, the answer to your question was literally 2 posts above your post - kinda - why not use 'replace' ?
Replace this with that...
Replace this with that...
Image
Simon Knight
Posts: 936
Joined: Wed Nov 04, 2009 11:41 am

Re: Finding the first space in a line of text

Post by Simon Knight »

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