Java String Line Count countLineBreaks(final String replacementString)

Here you can find the source of countLineBreaks(final String replacementString)

Description

count Line Breaks

License

Open Source License

Return

the number of line breaks in the passed string.

Declaration

public static int countLineBreaks(final String replacementString) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  ww w  . j a v  a 2 s  .co m
     * @return the number of line breaks in the passed string.
     */
    public static int countLineBreaks(final String replacementString) {
        int lineBreaks = 0;
        int ignoreNextNAt = -1;

        //we may have line breaks with \r\n, or only \n or \r
        final int len = replacementString.length();
        for (int i = 0; i < len; i++) {
            char c = replacementString.charAt(i);
            if (c == '\r') {
                lineBreaks++;
                ignoreNextNAt = i + 1;

            } else if (c == '\n') {
                if (ignoreNextNAt != i) {
                    lineBreaks++;
                }
            }
        }
        return lineBreaks;
    }
}

Related

  1. countLine(CharSequence str)
  2. countLine(String content)
  3. countLine(String contents, int index)
  4. countLineBreaks(String text)
  5. countLineEnds(CharSequence string, int startPos)
  6. countLines(final String content, final String lineBreak)
  7. countLines(String aMessage)