Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi All,
 
I have a string I need to reverse. I was looking for the simplest way (as always!)
so have tried the string to see if that exists, it doesn't or I can't find it.
MSDN have lots of suggestions along the lines of
Console.WriteLine("Pleae enter a string, print enter to end");
string input = Console.ReadLine();
StringBuilder sb = new StringBuilder();
int i = input.Length-1;
while (i != - 1)
{
      sb.Append(input[i--]);
}
Console.WriteLine("Reverse stirng is " + sb.ToString());
which seems to be a the correct way of doing it I just now
need to get the sb the string builder "variable" to equal a string for the next bit of editing it need as a string for the next bit of processing.
if I do sb = Next (Next is a string) I get the Error Cannot implicitly convert type 'string' to 'System.Text.StringBuilder'. So two questions is this the right way, if so how do I get a string out it, if not what is the right way?
Glenn
Posted 22-Jan-13 5:20am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

LINQ could be another approach you could consider.
String Reversal[^] discusses three approaches to reverse a string.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 22-Jan-13 11:45am
   
Nice and kinda comprehensive, a 5.
—SA
Abhinav S at 22-Jan-13 11:56am
   
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Did you try searching? This was the first hit for 'c# string reverse' and answers your question. It seems the Array.Reverse approach is the best.
  Permalink  
Comments
glennPattonWork at 22-Jan-13 11:34am
   
Only on MSDN really, found a method and tried sb.ToString() sorted out my problem, I will have a look at that now....

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

  Print Answers RSS
0 CPallini 465
1 Sergey Alexandrovich Kryukov 310
2 George Jonsson 269
3 Gihan Liyanage 255
4 OriginalGriff 245
0 OriginalGriff 4,903
1 CPallini 3,960
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,786
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100