我有一个样本字符串
"ZZZ-1234567890-from ABC-2 DEF"
我需要的输出是:
"from ABC-2 DEF"所有示例字符串都是格式的。
"ZZZ-numbers-Strings"
我得把绳子的部分弄完
"ZZZ-numbers-"
我试过使用split(),但是解决不了。感谢你的帮助!
发布于 2015-06-12 14:55:36
更新
在再次阅读您的问题后,知道您的String是格式的。
“ZZZ-数字-字符串”,你想要“ZZZ-数字-”之后的一切
然后,您可以使用正则表达式"ZZZ-\d+-“进行拆分。这将导致一个2元素数组,并且您希望在索引1处得到结果。
public static void main(String[] args) throws Exception {
String data = "ZZZ-1234567890-from ABC-2 DEF";
String[] split = data.split("ZZZ-\\d+-");
System.out.println(split[0]);
System.out.println(split[1]);
}结果:
(blank line for split[0])
from ABC-2 DEF旧答案
这看起来更像是substring()和indexOf()的任务,只要"from“在String中只存在一次,并且您希望在"from”包括"from“之后捕获所有内容。
public static void main(String[] args) throws Exception {
String data = "ZZZ-1234567890-from ABC-2 DEF";
System.out.println(data.substring(data.indexOf("from")));
}结果:
来自ABC-2 DEF
发布于 2015-06-12 15:12:06
根据您的格式,您可能要删除第一部分“ZZZ-numbers”。
使用replaceAll与regex一起执行这个技巧(受@Shar1er80答案的启发):
public static void main(String[] args) {
String data = "ZZZ-1234567890-from ABC-2 DEF";
System.out.println(data.replaceAll("^.{3}[-][0-9]+[-]",""));
}发布于 2015-06-12 15:06:39
简单,使用String.split("ZZZ-\\d+-")[1]
https://stackoverflow.com/questions/30805381
复制相似问题