Here you can find the source of getLineAtOffset(String text, int offset)
public static int getLineAtOffset(String text, int offset)
//package com.java2s; /******************************************************************************* * Copyright (c) 2007, 2008 Edgar Espina. * 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 * //from ww w. j av a 2 s . co m *******************************************************************************/ public class Main { public static int getLineAtOffset(String text, int offset) { // ISourceLineTracker lineTracker = TextUtils.createLineTracker(text); // return lineTracker.getLineNumberOfOffset(offset) + 1; int line = 1; for (int i = 0; i < offset; i++) { if (text.charAt(i) == '\n') { line++; } } return line; } }