Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trim @domain.xxx from email leaving just username

Tags:

java

I'm trying to trim the @domain.xxx from email address leaving just the username. I'm not sure how to dynamically select the @ position and everything to the right of it. Could someone please provide an example of how to do this? The trim code below is where I'm lost.

email = "[email protected]"
email....(trim code);
email.replace(email, "");
like image 855
Code Junkie Avatar asked Apr 30 '12 15:04

Code Junkie


2 Answers

To find: int index = string.indexOf('@');

To replace: email = email.substring(0, index);

To summarize:

email = "[email protected]";
int index = email.indexOf('@');
email = email.substring(0,index);
like image 147
gcochard Avatar answered Oct 21 '22 15:10

gcochard


Another approach is to split an email on a nickname and on a domain. Look at javadoc

There is a code example:

String email = "[email protected]";
String[] parts = email.split('@');

// now parts[0] contains "example"
// and parts[1] contains "domain.com"
like image 11
Gaim Avatar answered Oct 21 '22 15:10

Gaim