Click here to Skip to main content
14,693,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string str = textBox1.Text;
textBox1.Text = "";
int i = str.Length;
for (int j = 0; j < i - 1; j++)
{
    textBox1.Text += Convert.ToString(str[i]);
}

Above is back sapce coding....when i am going to run than error "Index was outside the bound of array" is coming
Posted
Updated 13-May-13 0:18am
v3

You have to convert the string into char array and then do foreach loop.

Example:

if (TextBox1.Text != string.Empty)
            {
                string str = TextBox1.Text;
                char[] array = str.ToCharArray();
                TextBox1.Text = "";

                foreach (char c in array)
                {
                    TextBox1.Text += Convert.ToString(c);
                }
            }
   
Comments
Mohammed Hameed 13-May-13 5:29am
   
If any queries let me know....
TanvirRaihan 13-May-13 5:36am
   
string itself is a array of char. Why you need to convert it?
Mohammed Hameed 13-May-13 5:49am
   
String is not array but it is a sequence of unicode chars.
Refer: http://msdn.microsoft.com/en-us/library/362314fe(v=vs.100).aspx

And check the below msdn link and see that even microsoft follows this pattern of converting string to char array. As far as I am aware this is how we deal with chars.
http://msdn.microsoft.com/en-us/library/2c7h58e5.aspx

If any more queries plz let me know.
sakshi111 13-May-13 6:31am
   
sir this code is not working....when i clicking at backspace than nothing is cahanging
Mohammed Hameed 13-May-13 7:05am
   
Please check the first if condition, Textbox.text should not be empty. If it is empty then nothing will change.
sakshi111 13-May-13 7:09am
   
no its not empty
MuhammadUSman1 14-May-13 1:05am
   
Dear Sakshi111!
I couldn't understand your question?

Do you want to remove last character of text in TextBox1?
Or
You Want to Show all text in reverse order.
Hi
Don't you think the code will be :

textBox1.Text += Convert.ToString(str[j]); 

instead of
textBox1.Text += Convert.ToString(str[i]);
   
Comments
Mohammed Hameed 13-May-13 5:28am
   
Ideally, the approach should be like the below solution...
sakshi111 13-May-13 5:34am
   
thank u it was minor mistake
Mohammed Hameed 13-May-13 5:38am
   
But ideally you have to use a char array in this scenario. You can verify the Solution 2 for example.
sakshi111 13-May-13 6:17am
   
why???

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900