Correction: when the question first came in the input string included line-feeds. It looks like now the question has been edited to remove the line-feeds. If they are no longer there then my solution won't work.
My first thought is to write a parsing routine that creates a map of key/value pairs. Then you can fetch the data pieces by name and the order in the string doesn't matter.
import java.util.HashMap;
import java.util.Map;
class Main
{
/**
* This method parses a string of "key:value" pairs into a map. Parameter
* pairs are separated by new-lines.
* @param in the string to parse
* @return the data map
*/
static Map<String,String> parseParameters(String in) {
Map<String,String> params = new HashMap<String,String>();
int pos = 0;
while(pos<in.length()) {
int i = in.indexOf("\n",pos);
if(i<0) i=in.length();
String sub = in.substring(pos,i);
pos = i+1;
int j = sub.indexOf(":");
if(j>=0) {
String key = sub.substring(0,j).trim();
String val = sub.substring(j+1).trim();
//System.out.println(key+":"+val);
params.put(key,val);
}
}
return params;
}
public static void main( String args[] )
{
String in = "#DESTINATION: 71222222\n#PRIORITY: urgent\n#AUTRE: rien";
Map<String,String> params = parseParameters(in);
// Get the data pieces by name
System.out.println(params.get("#AUTRE"));
System.out.println(params.get("#DESTINATION"));
System.out.println(params.get("#PRIORITY"));
}
}
.split(" ").