开发者

Delete one part of the String

开发者 https://www.devze.com 2023-03-27 03:01 出处:网络
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 cho

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

精彩评论

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