[英]How to get the last token from the string split by a pattern
I am having a string我有一个字符串
String S1="Hello@world@today@2018/01/01"
S2="Foo@Obj@new@tornado"
I want to get the last token always from each of these strings.我想总是从这些字符串中的每一个中获取最后一个标记。 Eg: 2018/01/01 in S1 and tornado in S2.例如:S1 中的 2018/01/01 和 S2 中的龙卷风。
Please help.请帮忙。
If @ is a common delimiter you could do如果@ 是一个常见的分隔符,你可以这样做
String[] parts = S1.split("@");
final String lastItem = parts[parts.length - 1];
Or, perhaps an easier way of doing it depending on your preference:或者,根据您的喜好,可能有一种更简单的方法:
final String lastItem = S1.substring(S1.lastIndexOf("@"));
You can use following function on String object for which you want last token您可以在需要最后一个令牌的 String 对象上使用以下函数
public static String getTocken(String s) {
String []str=s.split("@");
return str[str.length-1];
}
Pass the input String and get the result.传递输入字符串并获得结果。
use regex and replaceAll使用正则表达式并replaceAll
.*@
S1.replaceAll(".*@", "");
S2.replaceAll(".*@", "");
output输出
2018/01/01
tornado
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.