只是字符串扮演...
String in = "magnum-opus.txt: File(\"/magnum-opus.txt\", iconName=\"page_white_text\", mightHaveThumbnail=false, numBytes=17734, humanSize=\"17.3 KB\", lastModified=\"2014/01/27 05:19:30 UTC\", clientMtime=\"2014/01/27 05:19:30 UTC\", rev=\"11e6c9e5e\")";
String insideParenth = in.substring(in.indexOf("(")+1, in.length()-1);
StringTokenizer tokenizer = new StringTokenizer(insideParenth, ", ");
while (tokenizer.hasMoreTokens()) {
Map<String,String> properties = new HashMap<String,String>();
String token = tokenizer.nextToken();
if(token.contains("=")){
token = token.replaceAll("\"", "");
String left= token.substring(0, token.indexOf("="));
String right=token.substring(token.indexOf("=")+1);
properties.put(left,right);
System.out.println("left:["+left+"] and right=[" + right + "]");
}
}
輸出是:
left:[iconName] and right=[page_white_text]
left:[mightHaveThumbnail] and right=[false]
left:[numBytes] and right=[17734]
left:[humanSize] and right=[17.3]
left:[lastModified] and right=[2014/01/27]
left:[clientMtime] and right=[2014/01/27]
left:[rev] and right=[11e6c9e5e]
編輯:
String titleWithPath, titleWithoutPath;
insideParenth = in.substring(in.indexOf("(")+1, in.length()-1);
titleWithPath = insideParenth.substring(insideParenth.indexOf("\"")+1, insideParenth.indexOf(",")-1);
System.out.println(titleWithPath); //Prints out: "/magnum-opus.txt"
String titleWithoutPath = insideParenth.substring(insideParenth.indexOf("/")+1, insideParenth.indexOf(",")-1);
System.out.println(titleWithoutPath); //Prints out: "magnum-opus.txt"
Java或C++請選擇一個 – BackSlash
要麼,我就是。需要一種方法來做:) – Javed
也許你應該嘗試使用字符串變得更好。這是一個很糟糕的藉口。 – Kayaman