Reverse loop

How can reverse text in a label control?

e.g.

abcdefgh

I want to display as: hgfedcba

Thanks

ayha

LVL 7
ayha1999Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
http://forums.asp.net/t/553411.aspx/1
/// <summary> 
    /// Reverse a string 
    /// http://weblogs.sqlteam.com/mladenp/archive/2006/03/19/9350.aspx 
    /// </summary> 
    /// <param name="str"></param> 
    /// <returns></returns> 
    public string Reverse(string str) 
    { 
        if (str == null) return (""); 
 
        // convert the string to char array 
        char[] charArray = str.ToCharArray(); 
        int len = str.Length - 1; 
        /* 
        now this for is a bit unconventional at first glance because there 
        are 2 variables that we're changing values of: i++ and len--. 
        the order of them is irrelevant. so basicaly we're going with i from  
        start to end of the array. with len we're shortening the array by one 
        each time. this is probably understandable. 
        */ 
        for (int i = 0; i < len; i++, len--) 
        { 
            /* 
            now this is the tricky part people that should know about it don't. 
            look at the table below to see what's going on exactly here. 
            */ 
            charArray[i] ^= charArray[len]; 
            charArray[len] ^= charArray[i]; 
            charArray[i] ^= charArray[len]; 
        } 
        return new string(charArray); 
    }

Open in new window

0
DhaestCommented:
Even easier: http://www.dotnetperls.com/reverse-string

public static string ReverseString(string s)
    {
	char[] arr = s.ToCharArray();
	Array.Reverse(arr);
	return new string(arr);
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

ayha1999Author Commented:
the function returns the reversed string but throwing an error after that

Line: 117
Error: Object doesn't support property or method 'addEventListener'

and in the debug

line 117 is
doc.addEventListener("DOMNodeInserted", onDOMNodeInserted, false);
0
ayha1999Author Commented:
I tried the vb function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Write(ReverseString("abcdefgh"))
    End Sub

    Function ReverseString(ByVal s As String) As String
        Dim arr() As Char = s.ToCharArray
        Array.Reverse(arr)
        Return New String(arr)
    End Function
0
ayha1999Author Commented:
I think the error is related the browser.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.