How to Reverse a String in C#: 4 Practical Methods
Reverse a C# string with Array.Reverse, LINQ, a StringBuilder loop, or Unicode-aware text elements. Learn the right method, null policy, and test cases.
A C# reverse string solution can be as short as converting the text to a char[], calling Array.Reverse, and creating a new string. For ASCII input, that is a sensible default. For names, accented text, or emoji, first decide what counts as one character.
This guide shows four useful approaches: an array, a LINQ expression, a backward StringBuilder loop, and Unicode-aware text elements. You will also see how to handle null, empty strings, whitespace, and word order without confusing those requirements with character reversal.
Quick answer: use Array.Reverse for simple text
For non-null ASCII text, start here:
using System;
string text = "Hello, C#!";
char[] characters = text.ToCharArray();
Array.Reverse(characters);
string reversed = new string(characters);
Console.WriteLine(reversed); // !#C ,olleH
Console.WriteLine(text); // Hello, C#!
ToCharArray() creates a mutable copy, Array.Reverse() changes the order of that array, and the string constructor creates the result. The original string stays unchanged. Microsoft documents string immutability and the behavior of Array.Reverse.
The important qualification is that a C# char is a UTF-16 code unit, not necessarily a complete visible character. This short method can split surrogate pairs and combining sequences. Skip to the StringInfo method when preserving user-perceived characters is the actual requirement.
Which method should you choose?
- Choose an array for a clear, reusable code-unit reversal, especially in a beginner exercise or with known ASCII data.
- Choose LINQ when a short expression fits the surrounding code. It has the same character-handling limitation as the array approach.
- Choose a backward loop when an exercise forbids a reverse helper or you want to show the indexing explicitly.
- Choose text elements for user-facing text that can contain combining accents, supplementary characters, or multi-part emoji.
The first three methods reverse char values. The fourth reverses extended grapheme clusters on modern .NET. Those are different contracts, not merely different spellings of the same algorithm. Pick the contract before comparing speed or line count.
Method 1: a reusable character-array solution
Here is a complete console program with an explicit null policy:
using System;
class Program
{
static string ReverseCodeUnits(string input)
{
if (input is null)
throw new ArgumentNullException(nameof(input));
char[] characters = input.ToCharArray();
Array.Reverse(characters);
return new string(characters);
}
static void Main()
{
Console.WriteLine(ReverseCodeUnits("Hello, C#!"));
Console.WriteLine(ReverseCodeUnits(""));
}
}
The first output line is !#C ,olleH; the second is empty. An empty string is valid input, so it should not be treated as an error. A single-character input also works without special handling.
With the .NET SDK installed, run dotnet new console -n ReverseStringDemo, replace the generated Program.cs with the program above, and run dotnet run --project ReverseStringDemo. The methods in later sections can be added inside the same Program class, with their using directives placed at the top of the file.
Notice that Array.Reverse returns void: it mutates the supplied array instead of returning a reversed array. Do not try to pass its return value directly into new string(...). Keeping the copy, reversal, and string construction on separate lines makes that distinction easy to see.
This method also preserves every space and punctuation mark as data. It does not trim the input, remove punctuation, or change letter case. A leading space becomes a trailing space because its position is reversed along with everything else.
Method 2: reverse a string with LINQ
If you prefer a one-line expression, import System.Linq:
using System;
using System.Linq;
string input = "Hello, C#!";
string reversed = new string(input.Reverse().ToArray());
Console.WriteLine(reversed); // !#C ,olleH
Here, Reverse() is a LINQ extension method, not an instance method declared by System.String. A string is enumerable as char values, so LINQ can produce those values in reverse order. ToArray() materializes the sequence, and new string(...) reconstructs text.
The distinction matters when debugging. input.Reverse() by itself is a sequence, not a string. Calling .ToString() on that sequence does not join its characters into the reversed text. Use the constructor shown above. Microsoft explains the sequence result and deferred execution in its Enumerable.Reverse documentation.
This concise version still reverses UTF-16 code units. LINQ does not automatically make the operation emoji-safe or accent-safe. It also allocates storage; fewer lines do not prove that it is faster than the array method. Choose it for clarity in context, not for an unmeasured performance claim.
Method 3: a backward StringBuilder loop
When you need to reverse a string in C# without calling Reverse, walk from the final index to zero and append each char:
using System;
using System.Text;
static string ReverseWithLoop(string input)
{
if (input is null)
throw new ArgumentNullException(nameof(input));
var result = new StringBuilder(input.Length);
for (int i = input.Length - 1; i >= 0; i--)
result.Append(input[i]);
return result.ToString();
}
The initial capacity is the input length because the result will contain the same number of UTF-16 code units. For empty input, the loop starts below zero and runs no iterations, returning an empty string. The method rejects null consistently with the array version.
StringBuilder is useful for repeated appends because strings are immutable. Repeatedly writing result += input[i] can create and copy many intermediate strings; it is a poor default for a growing result. The StringBuilder API documentation describes its mutable buffer and append operations.
Do not copy Java syntax and expect new StringBuilder(input).Reverse() to work with the standard .NET builder. Java has a built-in builder reversal method; C# uses different APIs. Our Java string reversal guide explains that language separately.
This loop is easy to explain in an interview, but it is not more Unicode-aware than Array.Reverse. Both process individual char values. A different loop direction cannot fix a mismatch between code units and visible characters.
Why Unicode changes the answer
.NET strings use UTF-16. There are three useful levels to distinguish:
- Code unit: one 16-bit
char. - Unicode scalar value: a code point other than a surrogate code point. Some scalar values need two
charvalues in UTF-16. - Grapheme cluster, or text element: a unit a reader generally perceives as one character. It may contain several scalar values.
For example, the emoji in A😀B occupies a surrogate pair. Reversing its two halves separately produces invalid ordering rather than keeping the emoji intact. A console may display replacement symbols, which can hide what actually went wrong.
An accent gives a second example: "a\u0301b" contains an a, a combining acute accent, and a b. Reversing individual code units produces "b\u0301a"; the accent no longer follows the original a. Even reversing complete scalar values would not solve that combining-mark problem.
The System.Text.Rune type represents scalar values and is useful when that is your intended unit. However, reversing runes can still separate an accent or a multi-part emoji. For user-perceived characters, segment into text elements instead. Microsoft provides a clear explanation of these levels in its introduction to character encoding in .NET.
Method 4: preserve text elements with StringInfo
This implementation collects complete text elements, reverses their order, and joins them without changing the internal order of each element:
using System;
using System.Collections.Generic;
using System.Globalization;
static string ReverseTextElements(string input)
{
if (input is null)
throw new ArgumentNullException(nameof(input));
var elements = new List<string>();
TextElementEnumerator enumerator =
StringInfo.GetTextElementEnumerator(input);
while (enumerator.MoveNext())
elements.Add(enumerator.GetTextElement());
elements.Reverse();
return string.Concat(elements);
}
Example calls:
Console.WriteLine(ReverseTextElements("A😀B"));
// B😀A
Console.WriteLine(ReverseTextElements("a\u0301b"));
// bá (the combining accent stays with a)
Console.WriteLine(ReverseTextElements("A👩🏽💻B"));
// B👩🏽💻A
The GetTextElementEnumerator API reads text elements without changing the source string. The list reversal changes the order of those element strings, rather than the char values inside them.
Runtime version matters. Starting with .NET 5, StringInfo and TextElementEnumerator implement extended grapheme-cluster segmentation in accordance with Unicode rules. .NET Framework and .NET Core 3.x and earlier use older logic that does not keep every cluster together. Microsoft documents this grapheme-enumeration change. Test the scripts and emoji your application actually accepts on its deployed runtime.
This is logical-order reversal, not optical mirror writing. It does not reflect glyph shapes, translate text, or override bidirectional rendering. Multilingual display requirements can therefore need more specification than “make the text look backwards.”
Null, empty strings, and whitespace: set a clear policy
Null and an empty string are not the same value. The reusable methods above throw ArgumentNullException for null and return an empty string for "". That contract makes missing input visible instead of silently converting it to valid but empty text.
If your application needs to preserve null, add that policy at the boundary and document it. If missing form input should become empty, make that conversion deliberately before calling the method. Avoid scattering different null behaviors across otherwise equivalent reversal functions.
Whitespace should also follow the requirement, not an accidental cleanup step. Character reversal moves spaces, tabs, and line endings along with the rest of the sequence. In particular, a raw code-unit reversal turns a Windows \r\n pair into \n\r. If you want to move complete lines from bottom to top, use a line-order operation instead; see our reverse line order guide.
Reversing words is a different operation
Character reversal of red green blue produces backwards letters. Word-order reversal should produce blue green red, with each word still readable. Here is a deliberately whitespace-normalizing version:
using System;
static string ReverseWordOrder(string input)
{
if (input is null)
throw new ArgumentNullException(nameof(input));
string[] words = input.Split(
(char[]?)null, StringSplitOptions.RemoveEmptyEntries);
Array.Reverse(words);
return string.Join(" ", words);
}
The null separator array tells Split to use whitespace delimiters. Empty entries are removed, and Join inserts a single ordinary space. As a result, leading and trailing whitespace, repeated spaces, tabs, and line breaks are not preserved. This is appropriate only when that normalization is intentional. The nullable annotation in the cast assumes a modern C# project with nullable syntax available.
Punctuation stays attached to its token. This small method is not a language-aware sentence parser. If exact whitespace placement is required, define whether separators stay in their original positions or travel with words, then tokenize accordingly. Our word-order reversal guide explains the distinction without requiring code.
Tests worth running before you ship
The implementations shown here were checked on .NET 8.0.33 with 28 passing assertions covering the main algorithms, null behavior, whitespace, and Unicode edge cases. A successful hello example alone is only a smoke test. Check these cases against the method you intend to deploy:
- Empty and one-character input:
""stays empty;"x"stays"x". - Spaces and punctuation:
"Hello, C#!"becomes"!#C ,olleH"with all four methods. - Surrogate pair:
"A😀B"becomes"B😀A"with the text-element method, not with rawcharreversal. - Combining accent:
"a\u0301b"becomes"ba\u0301"with the text-element method. - Joined emoji:
"A👩🏽💻B"should keep the complete emoji together on your modern .NET runtime. - Null: the reusable methods should throw the documented exception.
- Word-order normalization: multiple spaces and a tab should collapse to single spaces only in the word-order method shown above.
Use exact assertions, such as string.Equals(actual, expected, StringComparison.Ordinal), rather than relying only on console appearance. Visually identical accented text can have different underlying representations, and unsupported console fonts can obscure a valid emoji. Keep normalization, case folding, and reversal as separate decisions.
For a palindrome exercise, also decide whether case, spaces, and punctuation count before comparing a value with its reversal. Our palindrome checking guide walks through those rules. Reversal alone is not a complete multilingual palindrome specification.
Performance and common mistakes
The array and backward-loop approaches do linear work in the number of UTF-16 code units and use linear additional storage when returning a new string. Reversing an existing array is an in-place operation on that array, but converting an immutable string into an array and back is not an in-place string transformation.
The LINQ method also needs buffering to return a reversed string. The text-element version stores a list and element strings, trading a more explicit allocation pattern for the required segmentation behavior. Do not choose a code-unit method merely because it has fewer allocations if it corrupts the data you need to preserve.
Avoid repeated string concatenation in a growing loop and recursive substring-and-concatenation tricks as production defaults. For a performance-sensitive path, benchmark representative inputs on your target .NET runtime, measure allocations as well as elapsed time, and compare implementations with the same reversal semantics. No single stopwatch result establishes the fastest method for every application.
The practical takeaway
Use ToCharArray, Array.Reverse, and new string when you deliberately want code-unit reversal or work with known ASCII input. LINQ offers a compact alternative, and a backward builder loop makes the algorithm explicit. For user-facing Unicode text on modern .NET, reverse text elements with StringInfo and test representative input.
If you only need to inspect a short sample, the online Text Reverser avoids setting up a project; an application still needs its own defined and tested text-handling contract. If the transformation belongs in a database query rather than C# application code, see SQL REVERSE. The most important choice is not the shortest expression: it is deciding exactly what should remain together when the order changes.
Try the Text Reverser tool
Reverse text, words, or letters instantly — free and entirely in your browser.
Open the tool