03-13-2024, 12:19 PM
Operating on specific lines in a multiline string can be quite useful. The following QM function comes from QM's extension resources.
Is there a similar member function in LA?
Member function str.ReplaceLineN
Member function str.RemoveLineN
Member function str.InsertLineN
Is there a similar member function in LA?
Member function str.ReplaceLineN
function# $replacement lineindex [nlines]
;Replaces specified line(s).
;Returns index of first character of lineindex-th line, or -1 if lineindex is too big.
;replacement - replacement string.
;lineindex - zero-based line index.
;nlines - number of lines to replace. Default or 0: 1 line.
;EXAMPLE
;str s="zero[]one[]two"
;s.ReplaceLineN("ONE" 1)
;out s
if(nlines<1) nlines=1
int i=findl(this lineindex)
if(i>=0)
,int j=findl(this+i nlines)
,if(j>=0) j-2; if(j<i or this[i+j]!=13) j+1
,this.replace(replacement i j)
ret i
Member function str.RemoveLineN
function# lineindex [nlines]
;Removes specified line(s).
;Returns index of first character of lineindex-th line, or -1 if lineindex is too big.
;lineindex - zero-based line index.
;nlines - number of lines to remove. Default or 0: 1 line.
;EXAMPLE
;str s="zero[]one[]two"
;s.RemoveLineN(1)
;out s
if(nlines<1) nlines=1
int i=findl(this lineindex)
if(i>=0) this.remove(i findl(this+i nlines))
ret i
Member function str.InsertLineN
function# $insertstring lineindex
;Inserts line.
;Returns index of first character of lineindex-th line, or -1 if lineindex is too big.
;insertstring - string to be inserted. Does not have to end with new line.
;lineindex - zero-based line index where to insert insertstring.
;EXAMPLE
;str s="zero[]one[]two"
;s.InsertLineN("" 1) ;;inserts empty line
;out s
int i=findl(this lineindex)
if(i<0 and numlines(this)=lineindex) this+"[]"; i=this.len
if(i>=0)
,str s.from(insertstring "[]")
,this.insert(s i s.len)
ret i