[英]Key Value parsing java
嗨,我有一條像“NameThoufiqAge21LocationTN”這樣的行,其中我的鍵是 Name、Age、Location 我正在尋找可以將上述行解析為鍵值對的代碼
姓名 Thoufiq 年齡 21 地點 田納西州
這只是一個例子,但它有效。 希望你能明白:
public static void main(String[] args) {
String str = "NameThoufiqAge21LocationTN";
Matcher matcher = Pattern
.compile("Name(\\w+)Age(\\d+)Location(\\w+)")
.matcher(str);
if (matcher.matches()) {
System.out.println("Name=" + matcher.group(1));
System.out.println("Age=" + matcher.group(2));
System.out.println("Location=" + matcher.group(3));
}
}
輸出:
Name=Thoufiq
Age=21
Location=TN
代碼
public class Main {
public static void main(String[] args) throws Exception {
String line = "NameThoufiqAge21LocationTN";
boolean newKey = true;
for (char a : line.toCharArray()) {
if (a < 97 && !newKey) { // http://www.asciitable.com/
newKey = true;
System.out.print(" " + a);
} else {
System.out.print(a);
newKey = false;
}
}
}
}
輸出
Name Thoufiq Age 21 Location TN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.