Java String Line Count countLines(String s)

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

Description

count Lines

License

Open Source License

Declaration

public static long countLines(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 University of Illinois at Urbana-Champaign and others.
 * All rights reserved. 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.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w ww.j ava 2s. c  o m
 *    UIUC - Initial API and implementation
 *******************************************************************************/

public class Main {
    public static long countLines(String s) {
        if (s.length() == 0)
            return 0L;

        long numLines = 1L;
        int lastIndex = 0;
        int nextIndex = s.indexOf('\n');
        while (nextIndex >= 0) {
            numLines++;
            lastIndex = nextIndex;
            if (lastIndex + 1 >= s.length())
                nextIndex = -1;
            else
                nextIndex = s.indexOf('\n', lastIndex + 1);
        }
        return numLines;
    }
}

Related

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