Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
String manipulation is a fundamental aspect of programming, and one common task is reversing an input string. In C#, there are several ways to accomplish this task such as while loop, each with its advantages, disadvantages, and best use cases. In this article, we will explore various methods to reverse a string or character array in C#, along with code examples for different scenarios and edge cases. Also, we will introduce an outstanding PDF generation library called IronPDF from Iron Software.
C# provides several built-in functions for string manipulation, and one of them is Array.Reverse(), which can be used to reverse an array of characters or a char array representing a string. Here's a code example of the reverse method:
public class Program
{
// static void main
public static void Main()
{
string original = "AwesomeIronPDF"; // string variable
char[] charArray = original.ToCharArray();
Array.Reverse(charArray);// static string reverse method
string reversed = new string(charArray); // reversed string
Console.WriteLine(reversed); // Output: FDPnorIemosewA
}
}
public class Program
{
// static void main
public static void Main()
{
string original = "AwesomeIronPDF"; // string variable
char[] charArray = original.ToCharArray();
Array.Reverse(charArray);// static string reverse method
string reversed = new string(charArray); // reversed string
Console.WriteLine(reversed); // Output: FDPnorIemosewA
}
}
Public Class Program
' static void main
Public Shared Sub Main()
Dim original As String = "AwesomeIronPDF" ' string variable
Dim charArray() As Char = original.ToCharArray()
Array.Reverse(charArray) ' static string reverse method
Dim reversed As New String(charArray) ' reversed string
Console.WriteLine(reversed) ' Output: FDPnorIemosewA
End Sub
End Class
StringBuilder
Another approach to reverse a string in C# is by utilizing the StringBuilder
class, which provides efficient string manipulation operations. Here's how you can use StringBuilder
to reverse a string:
public class Program
{
public static void Main()
{
string someText = "AwesomeIronPDF"; // string variable
StringBuilder sb = new StringBuilder();
for (int i = someText.Length - 1; i >= 0; i--)
{
sb.Append(someText[i]);
}
string reversed = sb.ToString(); // reversed string variable
Console.WriteLine(reversed); // Output: FDPnorIemosewA
}
}
public class Program
{
public static void Main()
{
string someText = "AwesomeIronPDF"; // string variable
StringBuilder sb = new StringBuilder();
for (int i = someText.Length - 1; i >= 0; i--)
{
sb.Append(someText[i]);
}
string reversed = sb.ToString(); // reversed string variable
Console.WriteLine(reversed); // Output: FDPnorIemosewA
}
}
Public Class Program
Public Shared Sub Main()
Dim someText As String = "AwesomeIronPDF" ' string variable
Dim sb As New StringBuilder()
For i As Integer = someText.Length - 1 To 0 Step -1
sb.Append(someText.Chars(i))
Next i
Dim reversed As String = sb.ToString() ' reversed string variable
Console.WriteLine(reversed) ' Output: FDPnorIemosewA
End Sub
End Class
A recursive approach can also be used to reverse a string in C#. This method involves recursively swapping characters from both ends of the string until the entire string is reversed. Here's an implementation:
public class Program
{
public static void Main()
{
string someText = "AwesomeIronPDF"; // random string
string reversed = ReverseString(someText); // reverse a string
Console.WriteLine(reversed); // Output: FDPnorIemosewA
}
// public static string reverse method
public static string ReverseString(string str)
{
if (str.Length <= 1)
return str;
return ReverseString(str.Substring(1)) + str[0];
}
}
public class Program
{
public static void Main()
{
string someText = "AwesomeIronPDF"; // random string
string reversed = ReverseString(someText); // reverse a string
Console.WriteLine(reversed); // Output: FDPnorIemosewA
}
// public static string reverse method
public static string ReverseString(string str)
{
if (str.Length <= 1)
return str;
return ReverseString(str.Substring(1)) + str[0];
}
}
Public Class Program
Public Shared Sub Main()
Dim someText As String = "AwesomeIronPDF" ' random string
Dim reversed As String = ReverseString(someText) ' reverse a string
Console.WriteLine(reversed) ' Output: FDPnorIemosewA
End Sub
' public static string reverse method
Public Shared Function ReverseString(ByVal str As String) As String
If str.Length <= 1 Then
Return str
End If
Return ReverseString(str.Substring(1)) + str.Chars(0)
End Function
End Class
When reversing strings, it's essential to consider edge cases to ensure robustness and correctness. Some edge cases to consider include:
Start by creating a Console application from Visual Studio.
Provide the Project name and Location.
Select the .NET version.
Install IronPDF to the created project.
It can also be done using the below command line.
dotnet add package IronPdf --version 2024.4.2
Write the below code to demonstrate String Reverse.
public class Program
{
public static void Main()
{
var content = "<h1>Demonstrate IronPDF with C# LinkedList</h1>";
content += "<h2>1. Using Array.Reverse Method</h2>";
string someText = "AwesomeIronPDF"; // new string variable
content += $"<p>Input String: {original}</p>";
char[] charArray = someText.ToCharArray();
Array.Reverse(charArray);// static string reverse method
string reversed1 = new string(charArray); // reversed string
Console.WriteLine(reversed1); // Output: FDPnorIemosewA
content += $"<p>output: {reversed1}</p>";
content += "<h2>2. Using StringBuilder</h2>";
StringBuilder sb = new StringBuilder();
content += $"<p>Input String: {original}</p>";
for (int i = someText.Length - 1; i >= 0; i--)
{
sb.Append(original[i]);
}
string reversed2 = sb.ToString();
content += $"<p>output: {reversed2}</p>";
content += "<h2>3. Using Recursive Approach</h2>";
content += $"<p>Input String: {someText}</p>";
string reversed3 = ReverseString(someText);
Console.WriteLine(reversed3); // Output: FDPnorIemosewA
content += $"<p>output: {reversed3}</p>";
// create Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from HTML string
var pdf = renderer.RenderHtmlAsPdf(content);
// Save to a file or Stream
pdf.SaveAs("reverseString.pdf");
}
public static string ReverseString(string str)
{
if (str.Length <= 1)
return str;
return ReverseString(str.Substring(1)) + str[0];
}
}
public class Program
{
public static void Main()
{
var content = "<h1>Demonstrate IronPDF with C# LinkedList</h1>";
content += "<h2>1. Using Array.Reverse Method</h2>";
string someText = "AwesomeIronPDF"; // new string variable
content += $"<p>Input String: {original}</p>";
char[] charArray = someText.ToCharArray();
Array.Reverse(charArray);// static string reverse method
string reversed1 = new string(charArray); // reversed string
Console.WriteLine(reversed1); // Output: FDPnorIemosewA
content += $"<p>output: {reversed1}</p>";
content += "<h2>2. Using StringBuilder</h2>";
StringBuilder sb = new StringBuilder();
content += $"<p>Input String: {original}</p>";
for (int i = someText.Length - 1; i >= 0; i--)
{
sb.Append(original[i]);
}
string reversed2 = sb.ToString();
content += $"<p>output: {reversed2}</p>";
content += "<h2>3. Using Recursive Approach</h2>";
content += $"<p>Input String: {someText}</p>";
string reversed3 = ReverseString(someText);
Console.WriteLine(reversed3); // Output: FDPnorIemosewA
content += $"<p>output: {reversed3}</p>";
// create Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from HTML string
var pdf = renderer.RenderHtmlAsPdf(content);
// Save to a file or Stream
pdf.SaveAs("reverseString.pdf");
}
public static string ReverseString(string str)
{
if (str.Length <= 1)
return str;
return ReverseString(str.Substring(1)) + str[0];
}
}
Public Class Program
Public Shared Sub Main()
Dim content = "<h1>Demonstrate IronPDF with C# LinkedList</h1>"
content &= "<h2>1. Using Array.Reverse Method</h2>"
Dim someText As String = "AwesomeIronPDF" ' new string variable
content &= $"<p>Input String: {original}</p>"
Dim charArray() As Char = someText.ToCharArray()
Array.Reverse(charArray) ' static string reverse method
Dim reversed1 As New String(charArray) ' reversed string
Console.WriteLine(reversed1) ' Output: FDPnorIemosewA
content &= $"<p>output: {reversed1}</p>"
content &= "<h2>2. Using StringBuilder</h2>"
Dim sb As New StringBuilder()
content &= $"<p>Input String: {original}</p>"
For i As Integer = someText.Length - 1 To 0 Step -1
sb.Append(original(i))
Next i
Dim reversed2 As String = sb.ToString()
content &= $"<p>output: {reversed2}</p>"
content &= "<h2>3. Using Recursive Approach</h2>"
content &= $"<p>Input String: {someText}</p>"
Dim reversed3 As String = ReverseString(someText)
Console.WriteLine(reversed3) ' Output: FDPnorIemosewA
content &= $"<p>output: {reversed3}</p>"
' create Renderer
Dim renderer = New ChromePdfRenderer()
' Create a PDF from HTML string
Dim pdf = renderer.RenderHtmlAsPdf(content)
' Save to a file or Stream
pdf.SaveAs("reverseString.pdf")
End Sub
Public Shared Function ReverseString(ByVal str As String) As String
If str.Length <= 1 Then
Return str
End If
Return ReverseString(str.Substring(1)) + str.Chars(0)
End Function
End Class
IronPDF library requires a license to execute applications. More info can be found on the IronPDF Licensing Information page.
A trial license can be obtained from the IronPDF Trial License page.
Paste the Key in the appSettings.json file below.
{
"IronPdf.License.LicenseKey" = "The Key Goes Here"
}
Reversing a string in C# is a common programming task with various approaches and considerations. Whether you prefer built-in functions, StringBuilder
, or recursive methods, each approach has its advantages, disadvantages, and best use cases. By understanding these methods and considering edge cases, you can effectively reverse strings in C# in a manner that suits your specific requirements. Choose the method that best fits your requirements based on performance, memory usage, and handling of special characters.
With the IronPDF library for C# PDF operations, developers can gain advanced skills to develop modern applications.
9 .NET API products for your office documents