๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ blogs โ€บ string-reverse-using-stringbuilder
How To Reverse String Using StringBuilder
November 6, 2019 - In this blog, I am going to explain the program for string reverse using StringBuilder.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ StringBuilder.html
StringBuilder (Java Platform SE 8 )
April 21, 2026 - public StringBuilder reverse() Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
๐ŸŒ
ByteHide
bytehide.com โ€บ blog โ€บ reversing a string in c#: step-by-step guide
Reversing a String in C#: Step-by-Step Guide (2026)
January 2, 2024 - In this code, weโ€™re initiating a StringBuilder class. We go through the original string character by character in reverse order (i = sampled.Length - 1; i >= 0; i--) and append each character to the StringBuilder.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ javas-stringbuilder-reverse-method-explained-b2b701ee2029
Javaโ€™s StringBuilder.reverse() Method Explained | Medium
October 3, 2024 - When you call the reverse() method, the StringBuilder internally uses a character-swapping algorithm. The process begins at the first and last characters of the string and swaps them.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ reverse-a-string
Reverse a String - GeeksforGeeks
class GfG { static String reverseString(String s) { StringBuilder res = new StringBuilder(); // Traverse on s in backward direction // and add each character to a new string for (int i = s.length() - 1; i >= 0; i--) { res.append(s.charAt(i)); } return res.toString(); } public static void main(String[] args) { String s = "abdcfe"; String res = reverseString(s); System.out.print(res); } } Python ยท
Published ย  March 7, 2026
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 7 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ StringBuilder.html
StringBuilder (Java Platform SE 7 )
public StringBuilder reverse() Causes this character sequence to be replaced by the reverse of the sequence. If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ stringbuilder_reverse.htm
Java StringBuilder reverse() Method
Using the reverse() method, we can reverse a string and this reversed string can be used to check whether it is a palindrome string or not. In the following example, we are creating an object of the StringBuilder class with the value "malayalam".
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ stringbuilder โ€บ .reverse()
Java | StringBuilder | .reverse() | Codecademy
August 22, 2022 - The .reverse() method returns a modified StringBuilder object with its character sequence rearranged in the opposite order.
๐ŸŒ
Code Maze
code-maze.com โ€บ home โ€บ how to reverse a string in c#
How to Reverse a String in C# - Code Maze
May 18, 2023 - Here, we iterate the input string stringToReverse character by character using a foreach loop, and we push each character onto a resultStack stack. This means that we add the characters to the stack in reverse order, effectively reversing the string. We create a new StringBuilder object to construct the reversed string by popping characters from the stack and appending them to the StringBuilder.
Top answer
1 of 2
1
The previous solution given will work! I'm going to suggest some modifications. Here's the original:public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.substring(i, i + 1); } return reversed;}That call to str.substring(i, i + 1) is essentially a way of picking out a single character at index i, but there's actually a built in String method for that: charAt. And I think things read a bit more easily using it. Using it is also a bit semantically clearer in my opinion (others can disagree!):public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.charAt(i); } return reversed;}It's generally better to use a StringBuilder, though, since that keeps us from generating a lot of Strings along the way (what if our input String was a very long String)?public static String reverse(String str) { StringBuilder reversed = new StringBuilder(); for (int i = 0; i < str.length(); ++i) { reversed.insert(0, str.charAt(i)); } return reversed.toString();}Note that here, I actually iterated forward through the String and prepended (using insert), but you could iterate backwards and use the append method, too. But the main point is that using a StringBuilder keeps us from having to generate new Strings for each character in the input.I hope that helps!
2 of 2
0
We can't use a pre-defined function in java that can instantaneously reverse a string. Therefore, we must come up with a way to do so. One way we can do this is by creating a new string adding individual characters to it by traversing through the original string in reverse order. This will result in the final string being the reverse of the original string, and this will be our solution to the problem. An implementation of this solution looks like:public class MyClass {public static void main(String args[]) {String before="hello";String after=reverse(before);System.out.println(after);}public static String reverse(String str){String reversed="";for(int i=str.length()-1; i>=0; i--){reversed+=str.substring(i,i+1);}return reversed;}}
๐ŸŒ
Reddit
reddit.com โ€บ r/csharp โ€บ efficient ways to reverse a string or int in c#?
r/csharp on Reddit: Efficient ways to reverse a string or int in C#?
May 18, 2020 -

I'm coming from Java, where I'm used to being able to tackle string-reversing problems with something as quick as this:

String palindrome = "level";
StringBuffer buffer = new StringBuffer(palindrome);
return buffer.reverse();

I've spent the past few hours studying the documentation of C# and looking for an equivalent, but StringBuilders don't seem to scratch this itch. The most I've found is reversing an array. This seems useful, but since I sometimes like to reverse a string to solve numerical problems (convert an int to a string, reverse the string, return the reversed int), it'd be handy to not have to rely on this.

Since I'm studying the topic, I thought I should at least ask now, am I essentially on the right track? Or is there something obvious that I might be missing?

Top answer
1 of 7
11
You said "efficient'. This is "concise". Code that consumes fewer lines isn't always better at performance. To know how "efficient" that method is in Java, we'd have to look at the source for the API implementation. The ways I can think of to efficiently reverse a string are these, not all of them are possible in C#: Allocate a new array the same size as the source and copy items into it. (O(N) CPU, O(N) memory). Allocate a temporary character and perform multiple swaps. (O(N) CPU, O(1) memory, not possible in C# because a string is immutable, any attempt to do this requires an additional O(N) allocation.) Recursion. (O(N) CPU, stack-based O(N) memory which is generally seen as cheaper than allocation. High risk of stack overflow as string sizes increase.) In C#, you could write an IEnumerator that iterates in reverse (and I'm pretty sure LINQ's Reverse() does this). That's not quite as efficient as recursion. It has to allocate a char or string for each iteration, and if your goal is an output array you still need an O(N) destination array. Basically in C# managed code there are no in-place string operations without copying to an intermediate char[] or StringBuffer and allocating a new final string. Womp womp. If someone can prove me wrong I will be ecstatic. But my understanding of the new superfluous low-level memory enhancements is they don't give us mutable strings, but instead good ways to reuse one buffer with reference semantics for many repeated string manipulations or read-only access.
2 of 7
8
var text = "hello world"; var reversed = string.Create(text.Length, text, (chars, state) => { var pos = 0; for (int i = state.Length -1 ; i >=0 ; i--) { chars[pos++] = state[i]; } }); Creating string with no allocation overhead using string.Create Method | text | Mean | Error | StdDev | Median | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | LinqReverse | Hello World | 209.03 ns | 0.747 ns | 0.583 ns | 208.80 ns | 1.00 | 0.0687 | - | - | 288 B | StringCreate | Hello World | 15.31 ns | 0.164 ns | 0.145 ns | 15.25 ns | 0.07 | 0.0115 | - | - | 48 B | Builder | Hello World | 74.22 ns | 1.186 ns | 0.926 ns | 74.49 ns | 0.36 | 0.0497 | - | - | 208 B | LinqReverse | Ipsum Lorem | 211.74 ns | 3.506 ns | 3.444 ns | 210.26 ns | 1.00 | 0.0687 | - | - | 288 B | StringCreate | Ipsum Lorem | 15.28 ns | 0.039 ns | 0.031 ns | 15.27 ns | 0.07 | 0.0115 | - | - | 48 B | Builder | Ipsum Lorem | 71.87 ns | 0.704 ns | 0.549 ns | 71.69 ns | 0.34 | 0.0497 | - | - | 208 B | LinqReverse | The b(...)y dog [42] | 502.79 ns | 9.722 ns | 10.402 ns | 495.38 ns | 1.00 | 0.1469 | - | - | 616 B | StringCreate | The b(...)y dog [42] | 41.98 ns | 0.623 ns | 0.486 ns | 41.97 ns | 0.08 | 0.0268 | - | - | 112 B | Builder | The b(...)y dog [42] | 240.02 ns | 0.408 ns | 0.319 ns | 239.95 ns | 0.48 | 0.1221 | - | - | 512 B [MemoryDiagnoser] public class StringReverseBench { [Params("Hello World", "The big brown fox jumped over the lazy dog", "Ipsum Lorem")] public string text ; [Benchmark(Baseline = true)] public string LinqReverse() => new string(text.Reverse().ToArray()); [Benchmark] public string StringCreate() { return string.Create(text.Length, text, (chars, state) => { var pos = 0; for (int i = state.Length - 1; i >= 0; i--) { chars[pos++] = state[i]; } }); } [Benchmark] public string Builder() { var sb = new StringWriter(); // uses underlying stringbuilder for (int i = text.Length - 1; i >= 0; i--) { sb.Write(text[i]); } return sb.ToString(); } }
๐ŸŒ
IronPDF
ironpdf.com โ€บ ironpdf blog โ€บ .net help โ€บ c# reverse string
Reversing a String in C# (Beginner-Friendly Guide)
April 15, 2026 - Using Array.Reverse Method</h2>"; string someText = "AwesomeIronPDF"; // New string variable content += $"<p>Input String: {someText}</p>"; char[] charArray = someText.ToCharArray(); // Convert string to character array Array.Reverse(charArray); // Reverse the character array string reversed1 = new string(charArray); // Create a new reversed string Console.WriteLine(reversed1); // Output: FDPnorIemosewA content += $"<p>Output: {reversed1}</p>"; content += "<h2>2. Using StringBuilder</h2>"; StringBuilder sb = new StringBuilder(); // Create a StringBuilder instance content += $"<p>Input String:
๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 799121242 โ€บ Doc5
Reverse String with StringBuilder in Java | PDF
Doc5 - Free download as Word Doc (.doc / .docx), PDF File (.pdf), Text File (.txt) or read online for free.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ reverse-a-string-in-java
Reverse a String in Java - GeeksforGeeks
Explanation: Each character of the string is appended in reverse order to a new string r, resulting in the reversed output. StringBuilder provides a built-in reverse() method, making string reversal quick and efficient.
Published ย  May 12, 2026
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ java.lang.stringbuilder.reverse
StringBuilder.Reverse Method (Java.Lang) | Microsoft Learn
Reverses the order of characters in this builder. [Android.Runtime.Register("reverse", "()Ljava/lang/StringBuilder;", "")] public Java.Lang.StringBuilder Reverse(); [<Android.Runtime.Register("reverse", "()Ljava/lang/StringBuilder;", "")>] member ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-reverse-a-string-in-c-sharp
How to reverse a string in C#
Line 6: We'll create a string variable with the name str and assign it a value Educative. Line 9: We declare a variable reversedStr, this will hold the output from Reverse method with the string str as an argument. This method reverses the string. Line 15: We initialize the instance of StringBuilder.