Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Line index from character index of multiline string
#1
I wonder whether there exists a function to obtain the line-index of a multi-line string, giving the index number of one of its characters. Thanks in advance.
#2
I don't know if it exists or not but in the meantime you could try this:

Function getLineIndex
Code:
Copy      Help
function ~inputStr #cIndex
str line
int maxLen=0
int charLen=0
int lineIndex=0

foreach line inputStr
,maxLen += line.len-1
;
;Handle case where charIndex is < 0 or > max string length
if (cIndex < 0) or (cIndex > maxLen)
,lineIndex=-1
,goto Done

foreach line inputStr
,charLen += line.len-1
,if(charLen < cIndex) lineIndex+1
,else break
;
;Done
ret lineIndex

for testing only:

Macro getLineIndex Test
Code:
Copy      Help
str s=
;This is line 1
;This is line 2
;This is line 3
;This is line 4
;This is line 5

int charIndex=10
rep 5
,out "char index = %i, line index = %i" charIndex getLineIndex(s charIndex)
,charIndex+12
#3
Many thanks indeed! Best regards.


Forum Jump:


Users browsing this thread: 1 Guest(s)