String Parsing in Java
In Awk you can easily find the position of a delimiter and then take all characters up to that point by using the index() and substr() functions. But how does that work in Java? Is there an easy way to parse a String and carry out these type of routine actions?
Here you go:
subtring method:
int start = 1;index method:
int end = 4;
String substr = "aString".substring(start, end); // Str
String string = "madam, i am Adam";Here is a live example I am working with where I need to parse the String into two variables as my webpage is passing me a parameter that is pipe-delimited:
// First occurrence of a c
int index = string.indexOf('a'); // 1
String str = "YES|12345";
int pipe = str.indexOf('|'); //4
String firstPart = str.substring(1,pipe-1);
String lastPart = str.substring(pipe+1);