I have a java method in which trying to parse a string where fields are delimited by char ^A. Sample string like below.
HDR^A1^A20220106^ATYPE^AXXX^AJAPAN^AUNIFORM^AHELP^AEXAMPLE^A
I have attempted to use apache ordinalIndexOf but so far not yet successful with the same.
Is there any other alternative approach available for this scenario?
public class HelloWorld{
public static int ordinalIndexOf(String str, String substr, int n) {
int pos = str.indexOf(substr);
while (--n > 0 && pos != -1)
pos = str.indexOf(substr, pos+1);
return pos;
}
public static void main(String []args){
//Just kept it here as I need to use standards.. not using in below code
String CTRL_A = Character.valueOf((char) 0x01).toString();
String str = "HDR^ABYE^A20220103065014^Agoogle.com_29958^ABUDDY^A1.0^A123456789012^AHAI^ABYE";
int position = ordinalIndexOf(str,"^A",6);
System.out.println(str.substring(0,position));
}
}
Expected Output String:
EVENT_HDR^ABYE^A20220103065014^Agoogle.com_29958^ABUDDY^A1.0^A123456789012