I have a string that saves user login name and I want to remove specific characters from that string,i want to remove "@gmail.com" and just have the name before the @, then save it as a new string?
How can I do this?
Here's an example, email can be any email address, not just gmail.com
public class Test {
public static void main(String[] args) {
String email = "nobody@gmail.com";
String nameOnly = email.substring(0,email.indexOf('@'));
System.out.println(nameOnly);
}
}
You can use regex
+ replaceAll
method of string for eliminate it
sample:
String s = "Rod_Algonquin@company.co.nz";
String newS = s.replaceAll("@(.*).(.*)", "");
System.out.println(newS);
will work on different sites extension.
if you want .org
, .net
, etc then you need to change the regex @(.*).(.*)
make sure the email format be correct then use "split" method to split the string from '@' character's position and use first portion of results.
var str = "username@amailserver.com";
var res = str.split("@");
var username = res[0];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.