I have this String :
String myStr = "[email protected]"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With