我有一个包含此内容的Java字符串:
Response: Success
Message: Extension Status
Exten: 1234
Context: from-sip
Hint: DS/5678
Status: 9
我想在此字符串中搜索密钥"Exten:"
和"Status:"
并检索相应的值,即"1234"
和"9"
。这样做的最佳方式是什么?
答案 0 :(得分:6)
您可以使用正则表达式获取它们(Pattern
类,MULTILINE
标志flor simple):
import java.util.regex.*;
public class RegexStrings {
public static void main(String[] args) {
String input = "Response: Success\n"+
"Message: Extension Status\n"+
"Exten: 1234\n"+
"Context: from-sip\n"+
"Hint: DS/5678\n"+
"Status: 9";
String exten = null;
String status = null;
Matcher m = Pattern.compile("^Exten: (.+?)$", Pattern.MULTILINE).matcher(input);
if (m.find()) {
exten = m.group(1);
}
Matcher m2 = Pattern.compile("^Status: (.+?)$", Pattern.MULTILINE).matcher(input);
if (m2.find()) {
status = m2.group(1);
}
System.out.println("Exten: "+exten);
System.out.println("Status: "+status);
}
}
输出:
Exten: 1234
Status: 9
如果您的字符串具有固定格式,则可以使用String#substring()
的组合,这可能更容易理解:
public static void main(String[] args) {
String input = "Response: Success\n"+
"Message: Extension Status\n"+
"Exten: 1234\n"+
"Context: from-sip\n"+
"Hint: DS/5678\n"+
"Status: 9";
String exten = input.substring(input.indexOf("Exten: ")+"Exten: ".length(), input.indexOf("Context: ")).trim();
String status = input.substring(input.indexOf("Status: ")+"Status: ".length(),input.length()).trim();
System.out.println("Exten: "+exten);
System.out.println("Status: "+status);
}
(相同输出)
答案 1 :(得分:0)
您可以使用String.split()方法,最少使用Regex。以下是通用的代码,并将给定的输入转换为 Map以便于编程访问到键值对:
import java.util.Map;
import java.util.TreeMap;
public class NameValueCreator {
public static void main(String[] args) {
String input = "Response: Success\nMessage: Extension Status\nExten: 1234\nContext: from-sip\nHint: DS/5678\nStatus: 9";
System.out.println(input);
//Convert given input to Map
Map<String, String> nameValuePairs = convertToMap(input);
System.out.println("------------- converted map -----------------");
System.out.println(nameValuePairs);
}
public static Map<String, String> convertToMap(String input) {
Map<String, String> nameValuePairs = new TreeMap<String, String>();
//Split by newline to split the input into lines, where each line represents one key value pair
String[] nameValueStrings = input.split("\\n");
for (String nameValueString : nameValueStrings) {
//Split by colon followed by space
String[] nameAndValue = nameValueString.split(": ");
nameValuePairs.put(nameAndValue[0].trim(), nameAndValue[1].trim());
}
return nameValuePairs;
}
}
以下是上述程序的输出:
Response: Success
Message: Extension Status
Exten: 1234
Context: from-sip
Hint: DS/5678
Status: 9
------------- converted map -----------------
{Context=from-sip, Exten=1234, Hint=DS/5678, Message=Extension Status, Response=Success, Status=9}
不确定是否确实要搜索“{”作为密钥的一部分,例如Exten:
,如果是,请将nameValueString.split(": ");
替换为nameValueString.split(" ");
答案 2 :(得分:0)
这将有效
public static void main(String args[]) {
String read = "Response: Success\n" +
"Message: Extension Status\n" +
"Exten: 1234\n" +
"Context: from-sip\n" +
"Hint: DS/5678\n" +
"Status: 9";
String[] arr;
arr = read.split("\n");
for (String i : arr) {
if (i.contains("Exten:") || i.contains("Status:")) {
System.out.println(i.split(" ")[1]);
}
}
}
答案 3 :(得分:0)
Java属性类具有内置解析器,用于由新行终止的此类名称值对。我们可以这样做:
属性p = properties.load(new ByteArrayInputStream(xxx ...); P.getProperty(你的钥匙);
这将使您的代码变得小巧可读。