开发者

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 ?


You just need to use substring having found the right index to chop at:

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 @. If you want the substring before the last @ you would write:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜