Page 1 of 1
Delet all characters after word x
Posted: Wed Feb 13, 2013 1:15 am
by Nakia
Hi,
Is there an easy way to delete all characters (including tab,return etc) after a certain word in a line of text.
The amount and content of characters either side of word 6 varies so applying a fixed delete (char 5 to 7 etc) won't work..
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 1:33 am
by sturgis
Use matchchunk to find the match. (you might look at wholematches in the dictionary too)
get matchchunk(whateverDataToSearch,"theWordToFind",tStartPosition,tEndPosition)
If a match is found the special variable - it - will contain true, false if the match fails. So if you have a match, tEndPosition will then contain the char position of the last char of the word that was matched.
To delete everything after that word, just
Code: Select all
delete char tEndPosition to -1 of whateverDataWasUsed -- whateverDataWasUsed is the container with the text you are trying to modify.
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 1:37 am
by Simon
Ok have lots to assume here.
Code: Select all
put "I am a rather fantastic person who enjoys an afternoon at the pub" into tLine
put word 1 to 6 of tLine into tLine
Is that what you mean?
Maybe:
Code: Select all
repeat for each line tLine in tText
put word 1 to 6 of line tLine & cr after tNewText
end repeat
delete last line of tNewText
put tNewText into tText
Simon
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 1:58 am
by Nakia
Thanks Sturgis,
I can use that and get TRUE on "it" but when I answer the tStart and tAnswer variables i get an empty return..
my word to find is a Variable if that matters?
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 2:08 am
by sturgis
if it is ALWAYS word 6, definitely go with simons answer.
If you don't know what word it will be, and you removing ALL data after the word (in a multi line set of text) then matchchunk or even wordoffset should work.
To test, I set up 2 fields and a button, Field 1 has some text.
Field 2 is for the output. \
In the button is the following code
Code: Select all
on mouseUp
put field 1 into tData --put the data into a variable so its faster
get matchchunk(tData,"(variable)",tStart,tEnd) -- look for match. Forgot that parens mark the start end end of the match position that will then go into tStart and tEnd
if it is true then delete char tEnd to -1 of tData -- if there was a match, remove everything to the end
put tData into field 2 -- put the modified text into field 2
end mouseUp
Using wordoffset is very similar
Code: Select all
on mouseup
put field 1 into tData
put wordOffset("variable",tData) into tOffset -- find the first occurrence
if tOffset > 0 then delete word (tOffset + 1) to -1 of tData -- if there was a match, the offset will be > 0 so delete starting at the word AFTER the match, to the end of the text
put tData into field 2 -- put the modified text into field 2
end mouseup
But as mentioned, if you need to do this for each single line (a line being delimited by return) definitely go with what simon says. *grin*
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 3:11 am
by Simon
oops I made a mistake...
Code: Select all
put word 1 to 6 of line tLine & cr after tNewText
should be
Code: Select all
put word 1 to 6 of tLine & cr after tNewText
But still not sure if I'm answering the question.
It did read to me like it was always word 6, but I get these things wrong often.
Simon
VGhpcyBzdGFydGVkIHdpdGggS2V2aW4gb2YgY291cnNlCldobyBzcG9rZSB0aWxsIGhpcyB2
b2ljZSB3YXMgaG9yc2UKV2l0aCBhIHByb21vdGlvbgpXZSdsbCBnZXQgbXVjaCBtb3JlIG1v
dGlvbgpUaGlzIHNvZnR3YXJlIHdpbGwgZ28gb3BlbiBzb3VyY2U=
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 3:52 am
by sturgis
I'm just glad I got to use the phrase "simon says"
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 4:06 am
by dunbarx
Another county heard from.
I assume it is not always word 6. This will do the trick yet another way, though there will be problems if the word in question occurs in more than one place. Fortunately, there are simple ways around that as well.
Given a field "yourField" with some text in it, put this in a button somewhere:
Code: Select all
on mouseUp
ask "delete text after which word?"
find it
delete char (word 4 of the foundChunk + 1) to -1 of fld "yourField"
end mouseUp
This likely needs firming up, as it makes assumptions about your text similar to the comment above. Write back...
Craig Newman
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 6:33 am
by Nakia
Thanks all,
ended up using Simon's suggestion, not really sure why I didnt look at it from that angle before..
Thanks again..
Re: Delet all characters after word x
Posted: Wed Feb 13, 2013 7:13 pm
by jacque
Just for another perspective, I'd probably do it this way:
Code: Select all
get wordOffset(tWordToFind,tText)
if it > 0 then delete word it+1 to -1 of tText