Twitter スクリーン ネームでいっぱいのテキスト ファイルを読み取ってデータベースに保存しようとしています。ScreenNames は 15 文字を超えることはできないため、チェックの 1 つで、名前が 15 文字を超えないようにします。
AmericanExpress をアップロードしようとすると、非常に奇妙なことが起こっていることがわかりました。
これは私のテキストファイルの内容です:
americanexpress
AmericanExpress
AMERICANEXPRESS
そして、これは私のコードです:
var names = new List<string>();
var badNames = new List<string>();
using (StreamReader reader = new StreamReader(file.InputStream, Encoding.UTF8))
{
string line;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
var name = line.ToLower().Trim();
Debug.WriteLine(line + " " + line.Length + " " + name + " " + name.Length);
if (name.Length > 15 || string.IsNullOrWhiteSpace(name))
{
badNames.Add(name);
continue;
}
if (names.Contains(name))
{
continue;
}
names.Add(name);
}
}
最初の americanexpress は 15 歳未満の長さのテストに合格し、2 番目は失敗し、3 番目は合格します。コードをデバッグし、AmericanExpress の 2 番目のループで名前にカーソルを合わせると、次のようになります。
そして、これはデバッグ出力です:
americanexpress 15 americanexpress 15
AmericanExpress 16 americanexpress 16
AMERICANEXPRESS 15 americanexpress 15
AmericanExpress の文字数を少なくとも 10 回数えましたが、15 文字しかないと確信しています。
Visual Studio が americanexpress.Length = 16 と表示する理由を知っている人はいますか?
解決
name = Regex.Replace(name, @"[^\u0000-\u007F]", string.Empty);