[英]java How to get the list from a string by the specified symbol?
example I have a string 例子我有一个字符串
"123<a>3213<b>3434343<c>,example <d><1><2><3>"
I want to get the inner text by the symbol "<>"
我想通过符号
"<>"
获得内部文本
how can I get the list [a,b,c,d,1,2,3] ??? 我如何获得列表[a,b,c,d,1,2,3] ???
您可以使用StringUtils.substringsBetween(String str,String open,String close) 。
String[] parts = StringUtils.substringsBetween("123<a>3213<b>3434343<c>,example <d><1><2><3>", "<", ">");
You want the text between <..>
. 您需要
<..>
之间的文本。 Use a Pattern
and Matcher
over the given String
while grouping the text between <..>
在
<..>
之间的文本分组时,在给定的String
使用Pattern
和Matcher
String text = "123<a>3213<b>3434343<c>,example <d><1><2><3>";
Pattern pattern = Pattern.compile("<(.*?)>"); // reluctant quantifier
Matcher matcher = pattern.matcher(text);
List<String> entries = new LinkedList<>();
while (matcher.find())
entries.add(matcher.group(1)); // group 0 is the whole match, we only want what's between <>
System.out.println(entries);
This prints 此打印
[a, b, c, d, 1, 2, 3]
Untested, hardly bullet proof, little error checking, but it should work. 未经测试,几乎不防弹,几乎没有错误检查,但应该可以。 Of course things could be better, this is just from memory.
当然,情况可能会更好,这仅来自内存。
public List<String> getThings(String source) {
char[] chars = source.toCharArray();
boolean capturing = false;
List<String> result = new ArrayList<String>();
String token = "";
for(char c : chars) {
if (!capturing) {
if (c == '<') { // found open delimiter, start capture.
capturing = true;
continue;
}
} else {
if (c == '>') { // Found closing delimiter, stop capture.
results.add(token);
token = "";
capturing = false;
continue;
}
token = token + c;
}
}
if (!scanning) {
throw new RuntimeException("Source string ended with missing closing '>'");
}
return results;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.