在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
字串操作是程式設計中的基本方面,其中一個常見的任務是反轉輸入字串。 在 C# 中,有幾種方法可以完成這個任務,例如 while 迴圈,每種方法都有其優點、缺點和最佳使用案例。 在本文中,我們將探討在 C# 中反轉字串或字元陣列的各種方法,並提供不同情境和邊界案例的代碼範例。 此外,我們將介紹一個出色的 PDF 生成庫,名為IronPDF 來自 Iron Software.
C#提供了幾個內建函數用於字串操作,其中之一是 Array.Reverse。(),可用來反轉字元陣列或表示字串的字元陣列。 以下是 reverse 方法的程式碼範例:
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
另一種在 C# 中反轉字串的方法是使用 StringBuilder
類別,它提供高效的字串操作。 以下是使用StringBuilder
反轉字串的方法:
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
在 C# 中也可以使用遞歸方法來反轉字符串。 此方法涉及遞歸地從字符串的兩端交換字符,直到整個字符串被反轉。 這裡是一個實現:
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
在反轉字符串時,必須考慮邊緣情況以確保健全性和正確性。 需要考慮的一些邊界情況包括:
首先從 Visual Studio 建立一個控制台應用程式。
請提供專案名稱和位置。
選擇 .NET 版本。
將IronPDF安裝到創建的項目中。
也可以使用以下指令來完成。
dotnet add package IronPdf --version 2024.4.2
編寫以下代碼來展示字串反轉。
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 程式庫需要授權才能執行應用程式。 更多資訊請參見IronPDF Licensing Information頁面。
測試許可證可以從IronPDF 試用許可證頁面。
將鍵粘貼到以下的 appSettings.json 文件中。
{
"IronPdf.License.LicenseKey" = "The Key Goes Here"
}
在 C# 中反轉字符串是一項常見的程式設計任務,有各種方法和考慮因素。 不論您偏好內建函數、StringBuilder
或遞迴方法,每一種方法都有其優點、缺點及最佳使用情境。 透過瞭解這些方法並考慮邊緣情況,您可以根據具體要求有效地在C#中反轉字串。 根據效能、記憶體使用和特殊字元處理來選擇最適合您需求的方法。
隨著IronPDF 函式庫用於 C# PDF 操作,開發人員可以獲得進階技能以開發現代應用程式。