Wintertree Software Inc.

Sentry Spelling Checker Engine for Java

Home Site index Contact us Catalog Shopping Cart Products Support Search

You are here: Home > Support > Sentry Spelling Checker Engine > Java SDK > Problems with newline characters


Problems with newline characters

Product: Sentry Spelling Checker Engine Java SDK

Description: When you check text obtained from a text component using StringWordParser, the cursor is off by one for each line of text.

Discussion: This problem is actually the result of a "quirk" in the text component. Some controls represent newlines (end of line sequences) internally as a single character, but externally as a two-character sequence ("\r\n"). For example, if the text component contains the words "three", "blind", and "mice", each word on a separate line, it stores the text internally like this:

three\n
blind\n
mice\n

However, if your application calls the component's getText() method, the component returns a String that looks like this:

three\r\n
blind\r\n
mice\r\n

If you pass this string to the StringWordParser constructor, StringWordParser will calculate word positions based on a two-character end of line sequence. For example, StringWordParser would report the zero-based position of "blind" as 7 (5 characters for "three" plus 2 for "\r\n"). If you then attempt to highlight or select "blind" in the text component using this position, the highlighting will start at the "l" in "blind" -- it will be off by one character.

Solution: The solution is to edit the String returned by the component's getText() method so it matches the component's internal representation of the text by replacing "\r\n" with "\n" (i.e., by deleting "\r"). The following Java code does this:

String text = theComponent.getText();
StringBuffer dst = new StringBuffer();
for (int i = 0; i < text.length(); ++i) {
if (text.charAt(i) == '\r' &&
i < text.length() - 1 && text.charAt(i + 1) == '\n') {
// Skip \r
++i;
}
dst.append(text.charAt(i));
}
text = dst.toString();


Home Site index Contact us Catalog Shopping Cart Products Support Search


Copyright © 2015 Wintertree Software Inc.