5 Replies Latest reply: Oct 3, 2008 3:35 AM by 807589 RSS

    How to remove end of line from string?

    807589
      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
        • 1. Re: How to remove end of line from string?
          807589
          I don't think your regex makes any sense since '$' is end-of-input not end-of-line and matches a 'gap' and not an actual character. "$+" as a regex just seems silly. Why not just use replaceAll("[\r\n]",""); ?
          • 2. Re: How to remove end of line from string?
            807589
            From javadoc:
            $      The end of a line

            If you were right, how could I get index 5 for string "hello\r\n"?

            Will the regex [\r\n]+ work for all types of end of line? On all systems?
            • 3. Re: How to remove end of line from string?
              807589
              What you're trying to do is remove one or more line separators from the end of a string ("\n", "\r", and "\r\n" each count as one line separator, but "\n\n" is two line separators). This is all you need to do:
              str = str.replaceAll("[\r\n]+$", "");
              "$" doesn't match any characters, line separators or otherwise; it matches the position at the end of the string. In MULTILINE mode, it also matches the position before a line separator, but it still doesn't match the separator itself.
              • 4. Re: How to remove end of line from string?
                807589
                I want to removed all line separators from the end of line.
                Your solution seems to work properly, but will it really work for all systems? (I mean: when somebody creates text in any text editor does my method always remove the end of line?)
                • 5. Re: How to remove end of line from string?
                  807589
                  agad wrote:
                  I want to removed all line separators from the end of line.
                  Your solution seems to work properly, but will it really work for all systems? (I mean: when somebody creates text in any text editor does my method always remove the end of line?)
                  For sure your approach will not work but as long as end-of-line is signified by a '\r' or a '\n' or a combination of both then it will work. I don't know of any system where end-of-line is not signified by a '\r' or a '\n' or a combination of both.