Skip to main content
Participant
July 13, 2023
해결됨

Search for a certain letter in the last line of a text with always different numbers of lines

  • July 13, 2023
  • 1 답변
  • 364 조회

I am trying to search for a certain letter in the last line of a text with always different numbers of lines. I have already managed the search for the letter, I am only missing the possibility to limit the search to the last line. Sometimes it is only one line and sometimes two or even more.

var t = sourceRectAtTime().top;
var h = sourceRectAtTime().height;
var y = t + h;

var txt = text.sourceText;
var find = txt.includes("p");

if(find == true){[value[0], y-5]} else {[value[0], y]}

 

I tried it with .split("r")[x]; but I can only select a certain line, which doesn't help if I don't know which one is the last.

var t = sourceRectAtTime().top;
var h = sourceRectAtTime().height;
var y = t + h;

var txt = text.sourceText;
var t1 = txt.split("r")[0];
var t2 = txt.split("r")[1];
var t3 = txt.split("r")[2];
var t4 = txt.split("r")[3];


var find = t2.includes("p");

if(find == true){[value[0], y-5]} else {[value[0], y]}

 

Thanks for helping me!

이 주제는 답변이 닫혔습니다.
최고의 답변: Dan Ebberts

Something like this maybe:

var t = sourceRectAtTime().top;
var h = sourceRectAtTime().height;
var y = t + h;

var txt = text.sourceText.split("\r");
y  -= txt[txt.length-1].indexOf("p") > -1 ? 5 : 0;
[value[0], y]

1 답변

Mylenium
Legend
July 13, 2023

The last line is simply the last entry in the array you create with .split():

 

mLast=txt.split("\r").length;

 

From there addressing the last line is simply mLast-1 to adjust for the different counting and that is the only line you need to test. Your syntax seems botched, though. a carriage return is \r, not just r.

 

Mylenium

Participant
July 13, 2023

Thank you very much for your answer. I'm not quite sure how to implement the line in my code, unfortunately. I don't have much experience with expressions yet. Can you give me an example?

Dan Ebberts
Community Expert
Community Expert
July 13, 2023

Something like this maybe:

var t = sourceRectAtTime().top;
var h = sourceRectAtTime().height;
var y = t + h;

var txt = text.sourceText.split("\r");
y  -= txt[txt.length-1].indexOf("p") > -1 ? 5 : 0;
[value[0], y]