Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to Parse String

say for example you have a string that has key values but the variables after it can change ex:

KEY1=variable1, KEY2=variable2, KEY3=variable3

What I want to know is what is the best way to extract variable1, variable2, and variable3. It would be nice if I knew the the substrings and got them each time, but I don't bc the variables can change. Note the keys do not change

like image 660
auwall Avatar asked May 10 '11 12:05

auwall


Video Answer


1 Answers

You can try this:

String str = "KEY1=variable1, KEY2=variable2, KEY3=variable3";
String[] strArr = str.split(",");
String[] strArr2;
for (String string : strArr) {
    System.out.println(string);  // ---- prints key-value pair
    strArr2 = string.trim().split("=");
    System.out.println(strArr2[1]);  // ---- prints value
}
like image 75
Harry Joy Avatar answered Oct 17 '22 06:10

Harry Joy