Here you can find the source of countLinesColumns(String text, int initialLinesCnt, int initialColumnsCnt)
Parameter | Description |
---|---|
text | String. |
initialLinesCnt | initial value of lines counter. |
initialColumnsCnt | initial value of columns counter. |
private static Map.Entry<Integer, Integer> countLinesColumns(String text, int initialLinesCnt, int initialColumnsCnt)
//package com.java2s; // License as published by the Free Software Foundation; either import java.util.AbstractMap; import java.util.Map; public class Main { /**//from ww w . j a v a 2 s . c o m * Count lines and columns (in last line) in text. * @param text * String. * @param initialLinesCnt * initial value of lines counter. * @param initialColumnsCnt * initial value of columns counter. * @return entry(pair), first element is lines counter, second - columns * counter. */ private static Map.Entry<Integer, Integer> countLinesColumns(String text, int initialLinesCnt, int initialColumnsCnt) { int lines = initialLinesCnt; int columns = initialColumnsCnt; boolean foundCr = false; for (char c : text.toCharArray()) { if (c == '\n') { foundCr = false; lines++; columns = 0; } else { if (foundCr) { foundCr = false; lines++; columns = 0; } if (c == '\r') { foundCr = true; } columns++; } } if (foundCr) { lines++; columns = 0; } return new AbstractMap.SimpleEntry<>(lines, columns); } }