Here you can find the source of countLines(String s)
public static long countLines(String s)
//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; } }