HP Forums
SUBSTR indexes in free42 - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: Not HP Calculators (/forum-7.html)
+--- Forum: Not quite HP Calculators - but related (/forum-8.html)
+--- Thread: SUBSTR indexes in free42 (/thread-17300.html)



SUBSTR indexes in free42 - mtern - 07-31-2021 01:01 PM

Hello, playing with the new string functions in free42 I notice that it seems SUBSTR uses an Y index starting from 0, while the X index starts from 1.
So to get the Nth character of a string or element of a list, you have N-1 in Y and N in X
Is it the expected behaviour?

Or maybe the intepretation is different: SUBSTR takes element from Y included to X excluded?

As a simple exercise, this is a reusable routine to get the nth string character or list element:

LBL "NTH"
FUNC 21
RCL ST X
1
-
X<>Y
SUBSTR
RTN

thank you
Mark


RE: SUBSTR indexes in free42 - Albert Chan - 07-31-2021 03:50 PM

(07-31-2021 01:01 PM)mtern Wrote:  Or maybe the intepretation is different: SUBSTR takes element from Y included to X excluded?

Yes. String index is 0-based, end excluded → string length = end - begin


RE: SUBSTR indexes in free42 - mtern - 08-01-2021 03:48 PM

(07-31-2021 03:50 PM)Albert Chan Wrote:  
(07-31-2021 01:01 PM)mtern Wrote:  Or maybe the intepretation is different: SUBSTR takes element from Y included to X excluded?

Yes. String index is 0-based, end excluded → string length = end - begin

Thank you for the clarification.
Mark