[英]Delete one part of the String
I have this String:我有这个字符串:
String myStr = "something.bad@foo.us"
I want to get just the "something.bad" from myStr
?我想从
myStr
获得“something.bad”?
You just need to use substring
having found the right index to chop at:您只需要使用
substring
找到正确的索引即可:
int index = myStr.indexOf('@');
// TODO: work out what to do if index == -1
String firstPart = myStr.substring(0, index);
EDIT: Fairly obviously, the above takes the substring before the first @
.编辑:很明显,上面的 substring 在第一个
@
之前。 If you want the substring before the last @
you would write:如果你想要 substring 在最后一个
@
之前你会写:
int index = myStr.lastIndexOf('@');
// TODO: work out what to do if index == -1
String firstPart = myStr.substring(0, index);
You can use您可以使用
String str = myStr.split("@")[0];
It will split the string in two parts and then you can get the first String
element它将字符串分成两部分,然后您可以获得第一个
String
元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.