If you want to delete a line in a paragraph in java you can use java.util.Scanner;
In the below example, I scan through each line and check if a particular pattern occurs as the start of that line and delete that line.
import java.util.Scanner;
public class PatternFinder
{
/**
* @param args
*/
public static void main(String[] args)
{
String a = "TEST/Line1" +
"\nTEST/Line3 TESTER/123456" +
"\nthis is TESTER/Line4" +
"\nCON/Line2 TESTER/123456 " +
"\nTEST/Line3 TESTER/123456";
System.out.println("Input String :\n****************************\n" + a +"\n***************************");
Scanner scanner = new Scanner(a);
String b = "";
try{
while(scanner.hasNextLine())
{
String line = scanner.nextLine();
if(!containsElmntWithStartPtrn(line,"TEST/" ,"TESTER/")){
b= b + line +"\n";
}
}
System.out.println("Output String :\n****************************\n" + b.trim() +"\n***************************");
}catch (Exception e){
System.out.println("Error in PatternFinder" + e);
}finally {
scanner.close();
}
}
public static boolean containsElmntWithStartPtrn(String line, String startStr,String elementStr)
{
boolean contains = false;
if((line.indexOf(startStr) != -1) && line.indexOf(startStr) < startStr.length()){
if(line.contains(elementStr)){
contains = true;
}
}
return contains;
}
}
In the below example, I scan through each line and check if a particular pattern occurs as the start of that line and delete that line.
import java.util.Scanner;
public class PatternFinder
{
/**
* @param args
*/
public static void main(String[] args)
{
String a = "TEST/Line1" +
"\nTEST/Line3 TESTER/123456" +
"\nthis is TESTER/Line4" +
"\nCON/Line2 TESTER/123456 " +
"\nTEST/Line3 TESTER/123456";
System.out.println("Input String :\n****************************\n" + a +"\n***************************");
Scanner scanner = new Scanner(a);
String b = "";
try{
while(scanner.hasNextLine())
{
String line = scanner.nextLine();
if(!containsElmntWithStartPtrn(line,"TEST/" ,"TESTER/")){
b= b + line +"\n";
}
}
System.out.println("Output String :\n****************************\n" + b.trim() +"\n***************************");
}catch (Exception e){
System.out.println("Error in PatternFinder" + e);
}finally {
scanner.close();
}
}
public static boolean containsElmntWithStartPtrn(String line, String startStr,String elementStr)
{
boolean contains = false;
if((line.indexOf(startStr) != -1) && line.indexOf(startStr) < startStr.length()){
if(line.contains(elementStr)){
contains = true;
}
}
return contains;
}
}