Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to remove end of line from string?

807589Oct 3 2008 — edited Oct 3 2008
Hello,
I'd like to remove ends of line from the string. I've tried:
    static final Pattern END_LINE_PATTERN = Pattern.compile("$+");
    
    strBuf.append(input);
    Matcher m = END_LINE_PATTERN.matcher(input);
    int startIndex = -1;
    int endIndex;
    while (m.find()) {
	startIndex = m.start();
	endIndex = m.end();
	if (endIndex == strBuf.length() - 1) break;
    }
    if (startIndex > -1) {
	strBuf.setLength(startIndex);
    }
For strings "hello\n" and "hello\r" it works properly, but for string "hello\n\n" I get first occurrence at index 6 (at second \n), so as the result I get "hello\n". For the string "hello\r\n" I get first occurrence at index 5 (it's OK), but the end index is 5 as well, and the next occurrence I get at index 7, which doesn't give me any sense.

Hope somebody can help me.
Agata
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 31 2008
Added on Oct 3 2008
5 comments
449 views