2009-08-27 107 views
2

清除URL的最佳方式是什麼?我要尋找一個URL這樣什麼是最好的方式來清理URL中的標題

what_is_the_best_headache_medication

我當前的代碼

public string CleanURL(string str) 
{ 
    str = str.Replace("!", ""); 
    str = str.Replace("@", ""); 
    str = str.Replace("#", ""); 
    str = str.Replace("$", ""); 
    str = str.Replace("%", ""); 
    str = str.Replace("^", ""); 
    str = str.Replace("&", ""); 
    str = str.Replace("*", ""); 
    str = str.Replace("(", ""); 
    str = str.Replace(")", ""); 
    str = str.Replace("-", ""); 
    str = str.Replace("_", ""); 
    str = str.Replace("+", ""); 
    str = str.Replace("=", ""); 
    str = str.Replace("{", ""); 
    str = str.Replace("[", ""); 
    str = str.Replace("]", ""); 
    str = str.Replace("}", ""); 
    str = str.Replace("|", ""); 
    str = str.Replace(@"\", ""); 
    str = str.Replace(":", ""); 
    str = str.Replace(";", ""); 
    str = str.Replace(@"\", ""); 
    str = str.Replace("'", ""); 
    str = str.Replace("<", ""); 
    str = str.Replace(">", ""); 
    str = str.Replace(",", ""); 
    str = str.Replace(".", ""); 
    str = str.Replace("`", ""); 
    str = str.Replace("~", ""); 
    str = str.Replace("/", ""); 
    str = str.Replace("?", ""); 
    str = str.Replace(" ", " "); 
    str = str.Replace(" ", " "); 
    str = str.Replace(" ", " "); 
    str = str.Replace("  ", " "); 
    str = str.Replace("  ", " "); 
    str = str.Replace("  ", " "); 
    str = str.Replace("  ", " "); 
    str = str.Replace("   ", " "); 
    str = str.Replace("   ", " "); 
    str = str.Replace("   ", " "); 
    str = str.Replace("   ", " "); 
    str = str.Replace("    ", " "); 
    str = str.Replace("    ", " "); 
    str = str.Replace(" ", "_"); 
    return str; 
} 
+5

這段代碼看起來不錯,在每日跆拳道... ;-) – 2009-08-27 19:12:42

+0

哈哈..大聲笑..我知道我知道我需要學習正則表達式 – user161433 2009-08-27 21:58:38

+1

正則表達式是不需要的。 Linq會做得更好。 (未過濾,(u,n)=> u.Replace(n,'')); – Dykam 2009-08-28 10:05:42

回答

0
  1. 你如何定義「友好」的URL - 我假設你的意思是刪除_的等
  2. 我想看看這裏的正則表達式。

如果你想用以上方法堅持,我會建議遷移到的StringBuilder在一個字符串。這是因爲你的每個替換操作都在創建一個新的字符串。

2

您應該考慮使用正則表達式來代替。它比你上面試圖做的更有效率。

更多關於正則表達式here

0

我可以收緊一條是:

while (str.IndexOf(" ") > 0) 
    str = str.Replace(" ", " "); 

...而不是你的" "更換無限多。但是你幾乎可以肯定需要一個正則表達式。

3

肯定的正則表達式:

public string CleanURL(string str) 
{ 
    str = Regex.Replace(str, "[^a-zA-Z0-9 ]", ""); 
    str = Regex.Replace(str, " +", "_"); 
    return str; 
} 

(實際上沒有測試過,把我的頭頂部)。

讓我解釋一下:

第一行刪除一切,不是一個字母字符(大寫或小寫)或空格。 第二行用單個下劃線替換任何空格序列(依次爲1或更多)。

+0

您的第一個正則表達式會吃掉空格。 – 2009-08-27 18:20:02

+0

固定,謝謝:) – 2009-08-27 18:21:17

+0

很酷。這看起來像我所擁有的,除了我更喜歡用連字符替換空格而不是下劃線。對於SEO,我認爲沒有區別。 – 2009-08-27 19:48:27

0

或者多一點冗長,但這種只允許字母數字和空格(被替換成「 - 」)

string Cleaned = String.Empty; 
foreach (char c in Dirty) 
    if (((c >= 'a') && (c <= 'z')) || 
     (c >= 'A') && (c <= 'Z') || 
     (c >= '0') && (c <= '9') || 
     (c == ' ')) 
      Cleaned += c; 
Cleaned = Cleaned.Replace(" ", "-"); 
3

通常最好的辦法是去同一個白名單正則表達式的方法而不是刪除所有不需要的字符,因爲你肯定會錯過一些。

這裏的答案很好,但我個人不想刪除帶有重音符號的變音符號和字符。所以,最終的解決方案,我想出了這個樣子的:

public static string CleanUrl(string value) 
{ 
    if (value.IsNullOrEmpty()) 
     return value; 

    // replace hyphens to spaces, remove all leading and trailing whitespace 
    value = value.Replace("-", " ").Trim().ToLower(); 

    // replace multiple whitespace to one hyphen 
    value = Regex.Replace(value, @"[\s]+", "-"); 

    // replace umlauts and eszett with their equivalent 
    value = value.Replace("ß", "ss"); 
    value = value.Replace("ä", "ae"); 
    value = value.Replace("ö", "oe"); 
    value = value.Replace("ü", "ue"); 

    // removes diacritic marks (often called accent marks) from characters 
    value = RemoveDiacritics(value); 

    // remove all left unwanted chars (white list) 
    value = Regex.Replace(value, @"[^a-z0-9\s-]", String.Empty); 

    return value; 
} 

的使用RemoveDiacritics方法是基於SO answer by Blair Conrad

public static string RemoveDiacritics(string value) 
{ 
    if (value.IsNullOrEmpty()) 
     return value; 

    string normalized = value.Normalize(NormalizationForm.FormD); 
    StringBuilder sb = new StringBuilder(); 

    foreach (char c in normalized) 
    { 
     if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark) 
      sb.Append(c); 
    } 

    Encoding nonunicode = Encoding.GetEncoding(850); 
    Encoding unicode = Encoding.Unicode; 

    byte[] nonunicodeBytes = Encoding.Convert(unicode, nonunicode, unicode.GetBytes(sb.ToString())); 
    char[] nonunicodeChars = new char[nonunicode.GetCharCount(nonunicodeBytes, 0, nonunicodeBytes.Length)]; 
    nonunicode.GetChars(nonunicodeBytes, 0, nonunicodeBytes.Length, nonunicodeChars, 0); 

    return new string(nonunicodeChars); 
} 

希望幫助別人通過slugifying URL和保持變音符號和朋友一起挑戰他們的URL友好等價物在同一時間。

0

的方式計算器是這樣做可以在這裏找到:

https://stackoverflow.com/a/25486/142014

優化速度(「這是第二個版本,展開了5倍以上的性能」)和大量的特殊照顧字符。

相關問題