Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Extension Methods to Reverse a String and StringBuilder Object

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
2 Jan 2011CPOL 9.2K   3
This was already decided here in another thread...Using unsafe code and just copying the chars you get much better performance...You could also XORcharArray[i] ^= charArray[len];charArray[len] ^= charArray[i];charArray[i] ^= charArray[len];But something like this is...
This was already decided here in another thread...

Using unsafe code and just copying the chars you get much better performance...

You could also XOR
C#
charArray[i] ^= charArray[len];
charArray[len] ^= charArray[i];
charArray[i] ^= charArray[len];



But something like this is actually faster due to less operations not allocations.

C#
public static unsafe void Reverse(ref String string)
{
    fixed(char* chars = string)
    {
        int c = 0, d = string.Length - 1;
        while(c < d)
        {
            char t = chars[d];
            chars[d] = chars[c];
            chars[c] = t;
            c++;
            d--;
        }
    }
}

<pre lang="cs">public static void Reverse(ref String string)
{
        char[] chars = string.ToCharArray();
        int c = 0, d = string.Length - 1;
        while(c &lt; d)
        {
            char t = chars[d];
            chars[d] = chars[c];
            chars[c] = t;
            c++;
            d--;
        }    
string = new string(chars);
}




License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Livin in a lonely world, caught the midnight train going anywhere... Only thing is it was a runaway train... and it ain't ever goin back...
мала ка на хари, Trahentes ex exsilium

Comments and Discussions

 
GeneralUnsafe string manipulation is a very bad idea: http://blogs.... Pin
Richard Deeming4-Jan-11 9:41
mveRichard Deeming4-Jan-11 9:41 
Unsafe string manipulation is a very bad idea:
http://blogs.msdn.com/b/brada/archive/2004/06/03/148144.aspx

For example, try this:

using System;

static class Test
{
const string A = "Hello!";

static void Main()
{
string B = A;
string C = "Hello!";

Feckit(B);

Console.WriteLine("A = {0}", A);
Console.WriteLine("B = {0}", B);
Console.WriteLine("A == B: {0}", A == B);
Console.WriteLine("C = {0}", C);
Console.WriteLine("A == C: {0}", A == C);
Console.WriteLine("\"Hello!\" = {0}", "Hello!");
Console.WriteLine("A == \"Hello!\": {0}", A == "Hello!");
Console.WriteLine("A == \"Yellow\": {0}", A == "Yellow");
}

unsafe static void Feckit(string value)
{
fixed(char* pA = value)
{
pA[0] = 'Y';
pA[5] = 'w';
}
}
}

GeneralEven without unsafe there is no denying that the methods I h... Pin
jfriedman2-Jan-11 12:17
jfriedman2-Jan-11 12:17 
GeneralI've never had to use unsafe code in my .Net apps. Speed is... Pin
#realJSOP1-Jan-11 23:23
mve#realJSOP1-Jan-11 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.