java 如何删除特定标记之前的所有内容,包括字符串中的标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14357500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove everything that comes before a particular token, including the token from a String
提问by Nigel Thomas
Possible Duplicate:
Remove a particular token from a string
可能的重复:
从字符串中删除特定标记
This question is a continuation to my earlier question
这个问题是我之前问题的延续
I need to remove whatever that comes before the "+" and the "+" from the String.
我需要从字符串中删除“+”和“+”之前的任何内容。
How can it be done?
怎么做到呢?
回答by PermGenError
Use String#substring()and String#indexOfin combination
使用字符串#子()和字符串的indexOf#结合
String s= "GUID+456709876790";
System.out.println(s.substring(s.indexOf("+")+1));
Output: 456709876790
输出:456709876790
回答by Jaco Van Niekerk
If the String is in the format +text+, a regular expression may do the trick:
如果字符串的格式为 +text+,正则表达式可能会起作用:
Pattern p = Pattern.compile(".*\+(.*)\+.*");
Matcher m = p.matcher("sdasdad+982347347+234234234");
if (m.matches()) {
System.out.println("Here you go: " + m.group(1));
}
The entire recognised string is group 0, the content in the first bracket is group 1.
整个识别出的字符串为0组,第一个括号中的内容为1组。