4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[C#] サロゲートペアを含む文字列で文字を個別に取り扱う

Posted at

サロゲートペアを含む文字列で、文字を個別に取り扱いたいときの方法メモ

Char型にしてしまうと、サロゲートペアをもつ文字(32bit)では2分割されてしまい、意図した文字ではなくなってしまう。
そのため System.Globalization.StringInfo.GetTextElementEnumerator(str) を使用する。

TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(s);
while (charEnum.MoveNext()) {
    string element = charEnum.GetTextElement();
}

参考

StringInfo.GetTextElementEnumerator メソッド (String) (System.Globalization)
Unicode のサロゲートペアとは何か - ひだまりソケットは壊れない
サロゲートペアや結合文字が含まれているか調べる: .NET Tips: C#, VB.NET
文字列から1文字取得する、文字列内の文字を列挙する: .NET Tips: C#, VB.NET

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?