Java String Replace getLineBreakOffsets(String replacementString)

Here you can find the source of getLineBreakOffsets(String replacementString)

Description

get Line Break Offsets

License

Open Source License

Declaration

public static List<Integer> getLineBreakOffsets(String replacementString) 

Method Source Code

//package com.java2s;
/**//from w w  w  .  j  a  v  a2 s.c o m
 * Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Eclipse Public License (EPL).
 * Please see the license.txt included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Integer> getLineBreakOffsets(String replacementString) {
        ArrayList<Integer> ret = new ArrayList<Integer>();

        int lineBreaks = 0;
        int ignoreNextNAt = -1;

        //we may have line breaks with \r\n, or only \n or \r
        for (int i = 0; i < replacementString.length(); i++) {
            char c = replacementString.charAt(i);
            if (c == '\r') {
                lineBreaks++;
                ret.add(i);
                ignoreNextNAt = i + 1;

            } else if (c == '\n') {
                if (ignoreNextNAt != i) {
                    ret.add(i);
                    lineBreaks++;
                }
            }
        }

        return ret;
    }
}

Related

  1. replace(final String text, final String search, final String replace)
  2. replace(String origStr, char oldChar, String newStr)
  3. replace(String source, Properties properties)
  4. replace(String source, String oldChars, String newChars)