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....)
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().
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.
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 = "!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With