2016-02-18 141 views
0

檢查字符串是否包含指定的Unicode字符的最佳方法是什麼?我的問題是我無法解析字符串/字符來格式\ u [byte] [byte] [byte] [byte]。我跟很多教程和線程這裏在計算器上,但是當我有方法,像這樣的:檢查字符串是否包含Unicode字符的暴怒

private bool ContainsInvalidCharacters(string name) 
{ 
    if (translation.Any(c => c > 255)) 
    { 
     byte[] bytes = new byte[name.Length]; 
     Buffer.BlockCopy(name.ToCharArray(), 0, bytes, 0, bytes.Length); 
     string decoded = Encoding.UTF8.GetString(bytes, 0, name.Length); 
     (decoded.Contains("\u0001")) 
     { 
      //do something 
     } 
} 

我得到這樣的輸出:「C:\ 0°\ 0N \0噸\ 0I \ 0N \0克\ 0U \0噸\ 0" 。

這真的不是我的一杯茶。我會很感激任何幫助。

+2

這是不完全清楚你正在嘗試做這裏...試着寫一步一步你想 – xanatos

+1

你有一個字符串做...什麼。 ..如果它有一些> 255個字符,你認爲它被嚴重解碼,所以你把它的一半拷貝到一個'byte []'(其中的一半,因爲char是2個字節)。然後你解碼爲UTF8 ...然後呢? – xanatos

+0

@xanatos好吧,我想要做的是檢測給定的字符串是否包含特定的unicode字符通過使用它的excaped形式。在第一步中,我遵循http://stackoverflow.com/questions/4459571/how-to-recognize-if-a-string-contains-unicode-chars,下一步我遵循http://stackoverflow.com/questions/ 472906 /轉換字符串到字節數組 - 不使用編碼 - 逐字節。但現在我發現這不是一種幸運的方法。 – Qerts

回答

2

這是你想要的嗎?

public static bool ContainsInvalidCharacters(string name) 
{ 
    return name.IndexOfAny(new[] 
    { 
     '\u0001', '\u0002', '\u0003', 
    }) != -1; 
} 

bool res = ContainsInvalidCharacters("Hello\u0001"); 

注意使用'\uXXXX':在'表示一個char而不是string

+0

是的。謝謝。我以爲我需要改變編碼... – Qerts

3

如果我想象的Unicode字符憤怒這將是我的選擇:

ლ(〜•︿•〜)つ︻̷┻̿═━一

所以要回答你的問題,那就是檢查字符串這樣憤怒你可以簡單地說:

private bool ContainsInvalidCharacters(string name) 
{ 
    return name.IndexOf("ლ(~•̀︿•́~)つ︻̷┻̿═━一") != -1; 
} 

;)

0

入住這也

/// <summary> 
    /// Check invalid character based on the pattern 
    /// </summary> 
    /// <param name="text">The string</param> 
    /// <returns></returns> 
    public static string IsInvalidCharacters(this string text) 
    { 
     string pattern = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]"; 
     var match = Regex.Match(text, pattern, ""); 
     return match.Sucess; 
    } 
相關問題