Java Scanner Usage removeNLinesForwardsFromStringMatch(String text, String match, int n)

Here you can find the source of removeNLinesForwardsFromStringMatch(String text, String match, int n)

Description

Removes the first n lines within a plain text from the position where the search string is located, inclusive.

License

Open Source License

Declaration

public static String removeNLinesForwardsFromStringMatch(String text, String match, int n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Scanner;

public class Main {
    /**/*from  www  . j a v  a2  s  . com*/
     * Removes the first n lines within a plain text from the position
     * where the search string is located, inclusive.
     */
    public static String removeNLinesForwardsFromStringMatch(String text, String match, int n) {
        Scanner scanner = new Scanner(text);
        String line = null, result = "";
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();

            if (line.matches("(" + match + ").*")) {
                int i = 0;
                while (scanner.hasNextLine() && i < n) {
                    scanner.nextLine();
                    i++;
                }
            }

            else
                result += line + "\n";
        }

        scanner.close();
        return result;
    }
}

Related

  1. processLine(String aLine)
  2. queryMenu(Scanner in, String msg, LinkedList options)
  3. queryStr(Scanner in, String msg)
  4. removeFirstNLines(String text, int n)
  5. removeIndexes(String xpath)
  6. selectProject(List projectList)
  7. splitLines(String string)
  8. splitToDouble(String input)
  9. stringToArray(String str)