+ 4

C# reverse string

Can you reverse a string using String builder class?

21st Mar 2021, 1:19 PM
Sanjay Kamath
Sanjay Kamath - avatar
1 Resposta
+ 4
Have you seen this? https://www.techiedelight.com/reverse-string-csharp/ It shows a few ways of reversing a string in c# including one that uses StringBuilder to append each character in reverse order. The following adapted version runs in Sololearn's Code Playground: using System; using System.Text; namespace SoloLearn { public static class Extensions { public static string reverse(this string str) { StringBuilder sb = new StringBuilder(); for (int i = str.Length - 1; i >=0 ; i--) { sb.Append(str[i]); } return sb.ToString(); } } class Program { public static void Main() { string str = "Reverse Me!!"; string reverse = str.reverse(); Console.WriteLine(reverse); } } }
21st Mar 2021, 4:03 PM
Josh Greig
Josh Greig - avatar