2015-03-02 34 views
0

我想修改一个现有的项目。请原谅我缺乏知识,我们的开发人员最近离开了这家公司,我试图解决一个问题,直到我们采用一个新的问题。解析一个'全名'字符串到一个电子邮件地址

这是一个基于其他信息发送电子邮件的webform应用程序。所有名称中使用以下格式之一:

  1. 名字姓氏
  2. 名字姓氏Lastname2
  3. 名字姓氏-Lastname2

我只是想处理字符串生成电子邮件地址。再次,这些遵循设置的格式,即[email protected]。我想通过找到第一个空间我可以孤立姓氏和简单地删除多余的空格或连字符,但我的代码似乎并没有工作。

我遇到的问题是,任何具有多个姓氏的人都会因应用程序获取的电子邮件地址错误而导致传送失败。例如:

传递失败这些收件人或组:Steven.Smith ([email protected]

如果只是名字史蒂芬·琼斯,它会正常工作,我有测试了几次。

我的代码:

string fn = FullName.Text.ToString(); 
string y = fn.Substring(0, fn.IndexOf(" ")).Trim().Replace(" ", ""); 
string z = fn.Substring(fn.IndexOf(" ") + 1).Trim().Replace(" ", ""); 
String ToAddress = y + "." + z + "@domain.com"; 

FullName的变量来自于该位的代码,我猜,这在Page_Load中类

System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent(); 
     string[] a = Context.User.Identity.Name.Split('\\'); 

System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]); 
     string Name = ADEntry.Properties["FullName"].Value.ToString(); 

有没有人对我怎么能实现任何想法我目标在这里?我很抱歉我的知识缺乏,但我对ASP.NET并不完全熟悉。从我的研究看来,我应该也许应该将字符串拆分成一个数组,但我无法弄清楚如何在程序中实现这一点。

也没有工作的原始代码如下。在我的例子中,我试图让它更有效率,并能够处理三个名字。

string myString1 = txtName.Text.ToString(); 
       int t = myString1.IndexOf(" ", 0); 

       //Extract the first name for from address 
       int q = t; 
       string fn1 = this.txtName.Text.ToString(); 
       string b = fn1.Substring(0,q); 
       //Extract the Last name for from address 
       int r= t; 
       string ln1 = this.txtName.Text.ToString(); 
       string c= fn1.Substring(r+1); 

谢谢!

+3

请出示一些例子输入和输出。 – CodeCaster 2015-03-02 15:01:55

+0

当两个人有相同的名字时会发生什么? – Jodrell 2015-03-02 15:50:54

+0

幸运的是,没有人拥有相同的名字。如果有的话,我们会通过添加一个数字或其他东西来使名称不同。 – 2015-03-02 16:52:46

回答

1

这不是完全清楚你的用户之一,有两个姓氏的电子邮件地址应该有什么样的格式,但它好像你应该能够做到

//Assuming example input "Stephen Smith-Jones" or "Stephen Smith Jones" 
//split on spaces and hyphens 
string[] names = FullName.Text.ToString() 
        .Split(new string[]{" ", "-"}, 
          StringSplitOptions.RemoveEmptyEntries); //in case of extra spaces? 

//[email protected] 
var email = String.Format("{0}.{1}{2}@domain.com", 
          names[0], 
          names[1], 
          (names.Length > 2) ? names[2] : ""); 

它是什么呢?将你的全名分解成一个名称数组。取第一个名字,第二个名字和(如果有的话)第三个名字并将它们放入电子邮件中。如果你想处理中间名或其他变体,你需要更复杂。

编辑:反映了[名字] [姓氏] // @ domain.com格式

+0

我们只有少数用户使用带连字符的名称,并且所有用户都输入了别名,格式为:[email protected](no连字符)。 第一条线是否应对斯蒂芬琼斯以及斯蒂芬史密斯琼斯? – 2015-03-02 15:32:07

+0

是;它会得到任何用户全名的第一个和最后一个名字。如果你有人命名为“詹姆斯伯爵史密斯伯爵艾琳顿兰斯顿史密斯惠特福德琼斯”,你会得到“[email protected]”请注意,如果你有一个后缀的人,它会抓住,作为姓氏。 “Arthur Foobar,博士”最终会变成“Arthur.Ph.D。@ domain.com”(并且该阵列实际上是“{”Arthur“,”Foobar“,”Ph.D。“}) – 2015-03-02 15:38:04

+0

是否可以修改这使得它的名字是:David Smith Robinson,电子邮件去了[email protected],而不是简单地选择第一个和最后一个名字?我确实尝试了你的代码作为测试,但它给了我一个关于'文本'不是有效属性的错误 – 2015-03-02 17:15:11

1

这样的事情?

 string fn = FullName.Text.ToString(); 
     string forename = fn.Split(' ')[0]; 
     string toAddress = forename + "." + fn.Substring(forename.Length + 1) + "@domain.com"; 
0

你不更换连字符:

更换

string z = fn.Substring(fn.IndexOf(" ") + 1).Trim().Replace(" ", ""); 

通过

string z = fn.Substring(fn.IndexOf(" ") + 1).Trim().Replace(" ", "").Replace("-", ""); 

此外,你应该在所有的检查无效的输入和无效邮件Adress-字符。例如,这可能是一个重音字符。

+0

我已经试过这个,但它仍然没有设法生成正确的电子邮件地址!输入无效会成为一个问题,但只有大约100个用户,其中没有一个在其电子邮件地址或姓名中有任何特殊字符。 – 2015-03-02 15:33:35

+0

请显示您尝试使用的输入以及相应的错误消息/输出 – swe 2015-03-02 15:38:08

0

您可以使用正则表达式和集团[lastname2?]:

Regex r = new Regex("(\\w+) (\\w+)[ -]?(\\w+)?"); 

string name = "John Anderson Johnson"; 
Match m = r.Match(name); 
if(m.Success) 
{ 
    string first_name = m.Groups[1].Value; 
    string last_name = m.Groups[2].Value; 
    string last_name2 = ((m.Groups.Count > 2 && !m.Groups[3].Value.Equals(String.Empty)) 
     ? m.Groups[3].Value : String.Empty); 

    // format as desired 
    string email = String.Format("{0}.{1}{2}@domain.com", first_name, last_name2); 
    string email2 = String.Format("{0}.{1}@domain.com", first_name, last_name); 
    Console.WriteLine(email); // outputs: [email protected] 
    Console.WriteLine(email2); // outputs: [email protected] 
} 

更新 源更新处理您的评论:

输入的别名格式为:[email protected](无连字符)

0

你可以先清理字符串像这样(我把它从this线程):

string cleanedString = System.Text.RegularExpressions.Regex.Replace(dirtyString,@"\s+"," "); 

然后你可以替换的空格和连字符用点:

cleanedString = cleanedString.Trim().Replace(" ", ".").Replace("-", "."); 

约翰Doe Doe2将成为John.Doe.Doe2

之后,您可以删除最后一部分:

然后
  if (cleanedString.IndexOf('.') != cleanedString.LastIndexOf('.')) 
      { 
       cleanedString = cleanedString.Substring(0, cleanedString.LastIndexOf('.')); 
      } 

其结果将是John.Doe

你也许可以做一个更好的办法...

相关问题