Page 1 of 1
Finding the first space in a line of text
Posted: Tue Jan 12, 2016 12:38 am
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
Re: Finding the first space in a line of text
Posted: Tue Jan 12, 2016 2:28 am
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
Re: Finding the first space in a line of text
Posted: Tue Jan 12, 2016 2:39 am
by quailcreek
Hi Dave,
Good to hear from you. Offset... brain freeze I did't think of that. Thanks a lot.
Re: Finding the first space in a line of text
Posted: Wed Feb 17, 2016 10:14 pm
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
Re: Finding the first space in a line of text
Posted: Wed Feb 17, 2016 10:26 pm
by Klaus
Hi Tom,
"repeat for each ..." is READ ONLY!
So you cannot modify N (in your example) without unexspected results, if at all!
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
Re: Finding the first space in a line of text
Posted: Wed Feb 17, 2016 10:38 pm
by quailcreek
Hi Klaus,
I appreciate the help. Same results. It replaces all of the spaces.
Re: Finding the first space in a line of text
Posted: Wed Feb 17, 2016 11:32 pm
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
Re: Finding the first space in a line of text
Posted: Wed Feb 17, 2016 11:57 pm
by quailcreek
That did it. Thanks Dick!
Find And Replace spaces in line of text
Posted: Fri Jun 14, 2019 8:28 pm
by gagsoft
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
Re: Finding the first space in a line of text
Posted: Fri Jun 14, 2019 9:21 pm
by bogs
Hehe, the answer to your question was literally 2 posts above your post - kinda - why not use 'replace' ?
- Replace this with that...
Re: Finding the first space in a line of text
Posted: Sat Jun 15, 2019 7:43 am
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