So I saw Jon's skeet video and there was a code sample :
所以我看到了Jon的双向飞碟视频,并且有一个代码示例:
There should have been a problem with the é
- after reversing but I guess it fails on .net2 (IMHO), anyway it did work for me and I did see the correct reversed string.
应该有一个问题é - 在倒车之后但是我猜它在.net2(恕我直言)上失败了,无论如何它确实对我有效并且我确实看到了正确的反转字符串。
char[] a="Les Misérables".ToCharArray();
Array.Reverse(a);
string n= new string(a);
Console.WriteLine (n); //selbarésiM seL
But I took it further:
但我进一步说:
In Hebrew there is the "Alef" char : א
在希伯来语中有“Alef”字符:א
and I can add punctuation like : אֳ
( which I believe consists of 2 chars - yet displayed as one.)
我可以添加标点符号:אֳ(我认为它由2个字符组成 - 但显示为一个。)
But now look what happens :
但现在看看会发生什么:
char[] a="Les Misאֳrables".ToCharArray();
Array.Reverse(a);
string n= new string(a);
Console.WriteLine (n); //selbarֳאsiM seL
There was a split...
有一个分裂......
I can understand why it is happening :
我能理解为什么会这样:
Console.WriteLine ("אֳ".Length); //2
So I was wondering if there's a workaround for this kind of issue in C# ( or should I build my own mechanism....)
所以我想知道在C#中是否存在针对此类问题的解决方法(或者我应该构建自己的机制......)
2 个解决方案
#1
36
The problem is that Array.Reverse
isn't aware that certain sequences of char
values may combine to form a single character, or "grapheme", and thus shouldn't be reversed. You have to use something that understands Unicode combining character sequences, like TextElementEnumerator:
问题是Array.Reverse不知道某些char值序列可能组合形成一个单独的字符,或“grapheme”,因此不应该反转。您必须使用能够理解Unicode组合字符序列的内容,例如TextElementEnumerator:
// using System.Globalization;
TextElementEnumerator enumerator =
StringInfo.GetTextElementEnumerator("Les Misאֳrables");
List<string> elements = new List<string>();
while (enumerator.MoveNext())
elements.Add(enumerator.GetTextElement());
elements.Reverse();
string reversed = string.Concat(elements); // selbarאֳsiM seL
#2
9
If you made the extension
如果你做了扩展
public static IEnumerable<string> ToTextElements(this string source)
{
var e = StringInfo.GetTextElementEnumerator(source)
while (e.MoveNext())
{
yield return e.GetTextElement();
}
}
you could do,
你能做到的,
const string a = "AnyStringYouLike";
var aReversed = string.Concat(a.ToTextElements().Reverse());
#1
36
The problem is that Array.Reverse
isn't aware that certain sequences of char
values may combine to form a single character, or "grapheme", and thus shouldn't be reversed. You have to use something that understands Unicode combining character sequences, like TextElementEnumerator:
问题是Array.Reverse不知道某些char值序列可能组合形成一个单独的字符,或“grapheme”,因此不应该反转。您必须使用能够理解Unicode组合字符序列的内容,例如TextElementEnumerator:
// using System.Globalization;
TextElementEnumerator enumerator =
StringInfo.GetTextElementEnumerator("Les Misאֳrables");
List<string> elements = new List<string>();
while (enumerator.MoveNext())
elements.Add(enumerator.GetTextElement());
elements.Reverse();
string reversed = string.Concat(elements); // selbarאֳsiM seL
#2
9
If you made the extension
如果你做了扩展
public static IEnumerable<string> ToTextElements(this string source)
{
var e = StringInfo.GetTextElementEnumerator(source)
while (e.MoveNext())
{
yield return e.GetTextElement();
}
}
you could do,
你能做到的,
const string a = "AnyStringYouLike";
var aReversed = string.Concat(a.ToTextElements().Reverse());