Find offset of first and last char of Word x in a field
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
- Contact:
Find offset of first and last char of Word x in a field
This probably has a simple answer but I can't find it!
I just need to find the offset of first and last char of say Word 5 in a field. How can I do that?
Kaveh
I just need to find the offset of first and last char of say Word 5 in a field. How can I do that?
Kaveh
Kaveh
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: Find offset of first and last char of Word x in a field
Hi Kaveh,
Here is a way, tested with "word" not tested with "trueword"
Best
Jean-Marc
Here is a way, tested with "word" not tested with "trueword"
Code: Select all
...
get word 1 to pNumW-1 of fld 1
put the num of chars of it +2 into tNumFirstC
put tNumFirstC + (the num of chars of word pNumW of fld 1) into tNumLastC
put tNumFirstC & "," & tNumLastC-1
...
Jean-Marc
https://alternatic.ch
Re: Find offset of first and last char of Word x in a field
Hi Kaveh,
As you are a fan of regex, here is my take on this:
and to use this function:
and a bit more generic:
Warning: this works for plain words and
needs some adjustments for trueWords or whatever they are.
consider this as an exercice for the reader...
Regards,
Thierry
As you are a fan of regex, here is my take on this:
Code: Select all
function forKaveh T
local Rslt
local Rx = "(?msx) (?:\w+?\s){4} (\w+) "
get matchChunk( T, Rx, p0, p1)
put "Indexes: " & p0 & " - " & p1 & cr into Rslt
// only for testing:
put char p0 to p1 of T after Rslt
return Rslt
end forKaveh
and to use this function:
Code: Select all
on mouseUp
local inputText
put line -8 to -2 of the script of me into inputText
answer forKaveh( inputText) &cr& word 5 of inputText
end mouseUp
/*
This probably has a simple answer but I can't find it!
I just need to find the offset
of first and last char of say Word 5 in a field.
How can I do that?
Kaveh
*/
and a bit more generic:
Code: Select all
function forKaveh T, n
local Rslt
local Rx = "(?msx) (?:\w+?\s){__N__} (\w+) "
replace "__N__" with ( n - 1) in Rx
get matchChunk( T, Rx, p0, p1)
....
needs some adjustments for trueWords or whatever they are.
consider this as an exercice for the reader...
Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
-
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
- Contact:
Re: Find offset of first and last char of Word x in a field
Thank you both.
I opted for J-M's method as it is to extend to truewords. (I failed to mention truewords was what I really needed.) Thierry's solution is in my regex library to study, especially (?msx), which I have not used.
It is probably not useful to put my code here as it is very specific, but basically the same method as Jean-Marc suggested. The only correction factor is that with truewords the first word will typically not start at char 1, so that needs to be dealt with.
Thank you both again.
Regards
Kaveh
I opted for J-M's method as it is to extend to truewords. (I failed to mention truewords was what I really needed.) Thierry's solution is in my regex library to study, especially (?msx), which I have not used.
It is probably not useful to put my code here as it is very specific, but basically the same method as Jean-Marc suggested. The only correction factor is that with truewords the first word will typically not start at char 1, so that needs to be dealt with.
Thank you both again.
Regards
Kaveh
Kaveh
Re: Find offset of first and last char of Word x in a field
@kaveh1000: I think you are looking for the 'charIndex' property. e.g. Given a field (1) containing "Hello World Again!":
This should work for any chunk - so trueWord too.
Code: Select all
put the charIndex of char 1 of word 2 of field 1 -> 7
put the charIndex of char -1 of word 2 of field 1 -> 11
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: Find offset of first and last char of Word x in a field
Hi,
Thanks Mark
"charindex" way works fine with "word" and "trueword"
Thanks Mark
"charindex" way works fine with "word" and "trueword"
https://alternatic.ch
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Find offset of first and last char of Word x in a field
Code: Select all
on mouseUp
put fld "fORIGIN" into TEKST
put word 5 of TEKST into W5
put char 1 of W5 into C1
put the last char of W5 into C2
put W5 && "is the 5th word in your text, and the first letter is" && C1 & ", and the last letter is" && C2 into fld "fRESULT"
set the textStyle of word 1 of fld "fRESULT" to bold
set the textColor of word 1 of fld "fRESULT" to red
set the textStyle of word 14 of fld "fRESULT" to bold
set the textColor of word 14 of fld "fRESULT" to red
set the textStyle of word 20 of fld "fRESULT" to bold
set the textColor of word 20 of fld "fRESULT" to red
end mouseUp
- Attachments
-
- Word 5.livecode.zip
- Here's the stack.
- (929 Bytes) Downloaded 233 times
-
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
- Contact:
Re: Find offset of first and last char of Word x in a field
Bingo!!
LC and LCMark do it again.
Yup, both these work correctly:
Thanks again Mark and others. Every answer adds to my knowledge and perhaps others' too...
Kaveh
LC and LCMark do it again.
Yup, both these work correctly:
Code: Select all
put char (charindex of trueword 4 of fld 1) of fld 1
put char (charindex of word 4 of fld 1) of fld 1
Kaveh
Kaveh
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: Find offset of first and last char of Word x in a field
Hi,
Using "Charindex" is defititively better than my first post which doesn't work if you have double space.
Jean-Marc
Using "Charindex" is defititively better than my first post which doesn't work if you have double space.
Jean-Marc
https://alternatic.ch