Java String Line Count countLines(String what)

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

Description

Get the number of lines in a file by counting the number of newline characters inside a String (and adding 1).

License

Open Source License

Declaration

static public int countLines(String what) 

Method Source Code

//package com.java2s;
/**//from ww w.  j  a  v  a 2 s  .  co m
 * Copyright (c) 2010 Ben Fry and Casey Reas
 *
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.opensource.org/licenses/eclipse-1.0.php
 */

public class Main {
    /**
     * Get the number of lines in a file by counting the number of newline
     * characters inside a String (and adding 1).
     */
    static public int countLines(String what) {
        int count = 1;
        for (char c : what.toCharArray()) {
            if (c == '\n')
                count++;
        }
        return count;
    }
}

Related

  1. countLines(String s)
  2. countLines(String str)
  3. countLines(String text)
  4. countLines(String text)
  5. countLines(String value)
  6. countLines(StringBuffer bigBuffer)
  7. countLineTypes(String line)