从两个字符之间获取字符串 [英] Get string from between two characters
本文介绍了从两个字符之间获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从两个字符之间获取字符串。我有这个
I need to get string from between two characters. I have this
S= "10:21:35 |Manipulation |Mémoire centrale |MAJ Registre mémoire"
并且它必须在变量中返回4个字符串:
and it have to return 4 strings each in a variable:
a=10:21:35
b=Manipulation
c=Mémoire centrale
d=MAJ Registre mémoire
推荐答案
String#split
。由于它接受正则表达式字符串,并且 |
是正则表达式中的特殊字符,因此您需要将其转义(使用反斜杠)。由于 \
是Java字符串文字中的特殊字符,因此您还需要转义 it ,人们有时会感到困惑。所以给定:
There's String#split
. Since it accepts a regular expression string, and |
is a special character in regular expressions, you'll need to escape it (with a backslash). And since \
is a special character in Java string literals, you'll need to escape it, too, which people sometimes find confusing. So given:
String S = "10:21:35 |Manipulation |Mémoire centrale |MAJ Registre mémoire";
然后
String[] parts = S.split("\\|");
int index;
for (index = 0; index < parts.length; ++index) {
System.out.println(parts[index]);
}
将输出
10:21:35
Manipulation
Mémoire centrale
MAJ Registre mémoire
( With 前三位的尾随空格; trim
如有必要。)
(With the trailing spaces on the first three bits; trim
those if necessary.)
这篇关于从两个字符之间获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文