// subtract the offset from the start of line, in order scanpos to be the position of the start of the line
int scanpos = caretpos-startpos;
// scan until a newline accumulating the line
String s = "";
try {
char ch = myDoc.charAt(scanpos);
while (ch!='\n') {
s += myDoc.charAt(scanpos);
scanpos += 1;
ch = myDoc.charAt(scanpos);
}