2014-07-06 21 views
-2

我開發了一個應用程序,它是基於用戶的應用程序。 int這個應用程序,如果用戶忘記了他的密碼,然後用戶可以重置他的密碼只能通過他的電子郵件ID和萬一如果用戶不記得他的電子郵件ID,這是與他的帳戶在那個時候我想要在社會安全號碼sha***[email protected]。我怎麼能達到這個,請幫助我解決這個問題,將不勝感激。謝謝。我想從電子郵件隱藏一些字符,如Sha***[email protected]

我已經得到這個代碼從stak over flow,但它不符合我的要求。這是良好的只有電話號碼不是電子郵件

public static string GetMaskedEmail(string number) 
{ 
    if (String.IsNullOrEmpty(number)) 
     return string.Empty; 

    if (number.Length <= 12) 
     return number; 

    string last12 = number.Substring(number.Length - 12, 12); 
    var maskedChars = new StringBuilder(); 
    for (int i = 0; i < number.Length - 12; i++) 
    { 
     maskedChars.Append(number[i] == '-' ? "-" : "#"); 
    } 
    return maskedChars + last12; 
} 

回答

3

工作是今晚有點慢,所以我解僱了Xamarin Studio和鞭打這件事給你。在最佳編碼實踐中,這不應該被視爲一個例子,實際上根本不是。

雖然這將提供一個運作的例子,您可以從中獲取並構建自己的方法,並希望在此過程中學習。一個很好的參考資源,如果你在閱讀任何代碼時迷路了,是MSDN,如果你還沒有訪問過,我會建議這樣做,併爲將來使用添加書籤。

using System; 

namespace EmailHash 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      if (args.Length <= 0) 
      { 
       Console.WriteLine ("No values were passed to application."); 
       return; 
      } 
      string email = args[0]; 

      int indexOfAt = email.IndexOf ("@"); 
      if (indexOfAt == -1) 
      { 
       Console.WriteLine("Unable to find '@' symbol within email."); 
       return; 
      } 

      int indexStart = 3; 
      int indexEnd = indexOfAt - 2; 
      if (indexStart >= indexEnd) 
      { 
       Console.WriteLine("Not enough characters in email to mask value."); 
       return; 
      } 

      string hashedEmail = email.Replace(email.Substring(indexStart, indexEnd - indexStart), "***"); 

      Console.WriteLine("Original email: " + email); 
      Console.WriteLine("Hashed email: " + hashedEmail); 
      return; 
     } 
    } 
} 
+0

謝謝你的幫助.. –

相關問題