java 从电子邮件中修剪@domain.xxx 只留下用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10386025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:49:08  来源:igfitidea点击:

trim @domain.xxx from email leaving just username

java

提问by Code Junkie

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.

我正在尝试从电子邮件地址中删除 @domain.xxx,只留下用户名。我不确定如何动态选择 @ 位置及其右侧的所有内容。有人可以提供一个如何做到这一点的例子吗?下面的修剪代码是我迷路的地方。

email = "[email protected]"
email....(trim code);
email.replace(email, "");

回答by gcochard

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

查找: int index = string.indexOf('@');

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

取代: email = email.substring(0, index);

To summarize:

总结一下:

email = "[email protected]";
int index = email.indexOf('@');
email = email.substring(0,index);

回答by Gaim

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

另一种方法是根据昵称和域拆分电子邮件。看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"