Need to trim the domain from email and make value max 10 digits if it is more than 10 and ignore '.' if came in between

huangapple go评论64阅读模式
英文:

Need to trim the domain from email and make value max 10 digits if it is more than 10 and ignore '.' if came in between

问题

以下是翻译好的内容:

要去除域名,我使用了以下条件:

if (sender.indexOf('@') != -1 ) {
     sender = sender.substring(0, sender.indexOf("@"));
}

这将去除域名:
123456789@gmail.com -> 123456789

现在我需要再添加一个条件,即它只应该采用最多10位数字,并去除域名。

例如:stack.overflow@gmail.com -> 应该得到stackoverf(最多10位数字)
在'.'之间,我们应该忽略?

我们该如何做到这一点?期待您的回复。

英文:

To strip domain I am using this condition:

if (sender.indexOf('@') != -1 ) {
     sender = sender.substring(0, sender.indexOf("@"));
}

This will strip domain:
123456789@gmail.com -> 123456789

Now I need to add 1 more condition that it should take max 10 digit only with strip domain.

For example: stack.overflow@gmail.com -> should give stackoverf(max 10 digit)
In betwwen '.' we should ignore ?

How can we do this? Appreciate your responses.

答案1

得分: 2

根据评论指出,更改为:

if (sender.indexOf('@') != -1 ) {
     sender = sender.substring(0, Math.min(sender.indexOf("@"), 10));
}

将确保在 10 个字符后切断名称。

英文:

As pointed out in the comments, changing to

if (sender.indexOf('@') != -1 ) {
     sender = sender.substring(0, Math.min(sender.indexOf("@"), 10));
}

will ensure the name will be cut after 10 characters

huangapple
  • 本文由 发表于 2020年10月19日 20:40:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64427543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定