Click here to Skip to main content
11,629,614 members (79,046 online)
Click here to Skip to main content

Tagged as

Extension Methods to Reverse a String and StringBuilder Object

, 1 Jan 2011 CPOL 3.7K
Rate this:
Please Sign up or sign in to vote.
Less code, and much faster, would be:public static string ReverseSB(string text) { int len = text.Length; if (len>1) { StringBuilder sb=new StringBuilder(text); int pivotPos=len/2; len--; for (int i = 0; i < pivotPos; i++) { int...
Less code, and much faster, would be:

public static string ReverseSB(string text) {
    int len = text.Length;
    if (len>1) {
        StringBuilder sb=new StringBuilder(text);
        int pivotPos=len/2;
        len--;
        for (int i = 0; i < pivotPos; i++) {
            int j=len-i;
            char temp=sb[i];
            sb[i]=sb[j];
            sb[j]=temp;
        }
        text=sb.ToString();
    }
    return text;
}


or its extension-method variant. Tested on a 80-char string it is 8 times faster. The reason should be clear: John's code creates 6 strings for each character pair, as each SubString, Remove, Insert method generates a new string object, copying the relevant characters.

Smile | :)

License

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

Share

About the Author

Luc Pattyn
Software Developer (Senior) Perceler
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.

You may also be interested in...

Comments and Discussions

 
GeneralAhh yes, you most certainly did. :) Pin
John Simmons / outlaw programmer1-Jan-11 5:45
mvpJohn Simmons / outlaw programmer1-Jan-11 5:45 
GeneralI did in the line <code>len--;</code> Pin
Luc Pattyn1-Jan-11 5:26
mvpLuc Pattyn1-Jan-11 5:26 
GeneralIn yours, j will create an exception on the first character ... Pin
John Simmons / outlaw programmer1-Jan-11 5:11
mvpJohn Simmons / outlaw programmer1-Jan-11 5:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150723.1 | Last Updated 1 Jan 2011
Article Copyright 2011 by Luc Pattyn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid