Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse a string with accent chars?

So I saw Jon's skeet video and there was a code sample :

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.

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 : א

and I can add punctuation like : אֳ ( which I believe consists of 2 chars - yet displayed as one.)

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....)

like image 236
Royi Namir Avatar asked Feb 22 '13 16:02

Royi Namir


People also ask

How do you reverse a character in a string in C#?

C# has a built-in function to reverse a string. First, the string is converted to a character array by using ToCharArray() then by using the Reverse() the character array will be reversed, But in this article, we will understand how to Reverse a String without using Reverse().

Can you use reverse () on a string?

String class does not have reverse() method, we need to convert the input string to StringBuffer, which is achieved by using the reverse method of StringBuffer.

How do I reverse a string in ES6?

Using Built-in Methods to Reverse the String - split(), reverse() and join() The simplest way to reverse a string in JavaScript is to split a string into an array, reverse() it and join() it back into a string. With ES6, this can be shortened and simplified down to: let string = "!


1 Answers

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:

// 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 
like image 118
Michael Liu Avatar answered Sep 18 '22 15:09

Michael Liu