Java:-The below mentioned code enables user to find a particular length of a string whenever required from a file(.txt file or something like that).
Example:-I have a requirement to extract 10 digit card number which was written like CardNumber:="'1234567890".
Note:-Put your sample txt file in workspace or modify file reader argument accordingly.
Code:-
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class PatternMatch {
public static void main(String[] args) {
try {
BufferedReader br= new BufferedReader(new FileReader("C:\\workspace\\newfile.txt"));
String line;
int lastindex=0;
try {
while((line=br.readLine())!=null){
while(lastindex!=-1){
lastindex = line.indexOf("CardNumber",lastindex);
if(lastindex!=-1){
String substr=line.substring(lastindex+14,lastindex+14+10);
System.out.println(substr);
lastindex=lastindex+14+10;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Example:-I have a requirement to extract 10 digit card number which was written like CardNumber:="'1234567890".
Note:-Put your sample txt file in workspace or modify file reader argument accordingly.
Code:-
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class PatternMatch {
public static void main(String[] args) {
try {
BufferedReader br= new BufferedReader(new FileReader("C:\\workspace\\newfile.txt"));
String line;
int lastindex=0;
try {
while((line=br.readLine())!=null){
while(lastindex!=-1){
lastindex = line.indexOf("CardNumber",lastindex);
if(lastindex!=-1){
String substr=line.substring(lastindex+14,lastindex+14+10);
System.out.println(substr);
lastindex=lastindex+14+10;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment