Click here to Skip to main content
15,867,568 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.88/5 (9 votes)
1 Jan 2011CPOL 17.9K   1   6
And even simpler, faster and better would be this:public static string ReverseCA(string s) { char[] chars=s.ToCharArray(); Array.Reverse(chars); return new string(chars);}Whatever the string's length, only three objects get created; and all characters get copied three...
And even simpler, faster and better would be this:

C#
public static string ReverseCA(string s) {
    char[] chars=s.ToCharArray();
    Array.Reverse(chars);
    return new string(chars);
}

Whatever the string's length, only three objects get created; and all characters get copied three times, no more, no less. For the same 80-char test string it is 14 times faster than John's original.

Note: if you have to reverse a StringBuilder, the other alternative is probably the better one.

:)

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)
Belgium Belgium
I am an engineer with a background in electronics, software and mathematics.

I develop technical software, both for embedded systems and for desktop equipment. This includes operating systems, communication software, local networks, image processing, machine control, automation, etc.

I have been using all kinds of microcontrollers and microprocessors (Intel 4004/8080/8051/80386/Pentium, Motorola 680x/680x0/ColdFire/PowerPC, Microchip PIC, Altera NIOS, and many more), lots of programming languages (all relevant assemblers, Fortran, Basic, C, Java, C#, and many more), and different operating systems (both proprietary and commercial).

For desktop applications and general development tools I have been using both UNIX systems and Mac/MacOS for many years, but I have switched to x86-based PCs with Windows, Visual Studio and the .NET Framework several years ago.

I specialize in:
- cross-platform development (making software that runs on diverse hardware/OS combinations)
- instruction set simulation
- improving software performance, i.e. making sure the software runs the job at hand in as short a time as possible on the given hardware. This entails algorithm selection, implementation design, accurate measurements, code optimisation, and sometimes implementing virtual machines, applying SIMD technology (such as MMX/SSE), and more.

Comments and Discussions

 
GeneralVisual Studio does east-coasting without any problem. There ... Pin
Luc Pattyn10-Jan-11 2:23
sitebuilderLuc Pattyn10-Jan-11 2:23 
GeneralThe compiler does not mind if you put all the method on one ... Pin
Toli Cuturicu10-Jan-11 2:02
Toli Cuturicu10-Jan-11 2:02 
GeneralWrong? The compiler is happy with it. I do that all the time... Pin
Luc Pattyn9-Jan-11 8:46
sitebuilderLuc Pattyn9-Jan-11 8:46 
GeneralOh, you put it in the wrong place, that's why I did not see ... Pin
Toli Cuturicu9-Jan-11 2:17
Toli Cuturicu9-Jan-11 2:17 
Oh, you put it in the wrong place, that's why I did not see it at first.
GeneralI don't see it (maybe because it really is missing??) Pin
Luc Pattyn4-Jan-11 7:19
sitebuilderLuc Pattyn4-Jan-11 7:19 
GeneralVery good! (except for a missing brace) Pin
Toli Cuturicu4-Jan-11 7:05
Toli Cuturicu4-Jan-11 7:05 

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.