Java String Line Count countLines(String text)

Here you can find the source of countLines(String text)

Description

Count the number of lines in a string

License

Open Source License

Parameter

Parameter Description
text The test

Return

the count

Declaration

static public int countLines(String text) 

Method Source Code

//package com.java2s;
/**/*from w ww .j a v a 2 s  .com*/
 * * $Id: Utils.java 28 2005-06-20 13:13:35Z rej $
 * * Copyright (c) 2002 Sun Microsystems, Inc.
 * *
 * * See the file "license.terms" for information on usage and redistribution
 * * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 **/

public class Main {
    /**
     * Count the number of lines in a string
     * @param text The test
     * @return the count
     */
    static public int countLines(String text) {
        if (text == null)
            return 0;

        int count = 1;
        char arr[] = text.toCharArray();
        for (char anArr : arr) {
            if (anArr == '\n')
                ++count;
        }

        return count;
    }
}

Related

  1. countLines(String input)
  2. countLines(String o)
  3. countLines(String s)
  4. countLines(String s)
  5. countLines(String str)
  6. countLines(String text)
  7. countLines(String value)
  8. countLines(String what)
  9. countLines(StringBuffer bigBuffer)