C# 反转字符串的最佳方法
我刚刚不得不在C#2.0中编写一个字符串反转函数(即LINQ不可用),并得出以下结论:C# 反转字符串的最佳方法,c#,.net,performance,algorithm,unicode,C#,.net,Performance,Algorithm,Unicode,我刚刚不得不在C#2.0中编写一个字符串反转函数(即LINQ不可用),并得出以下结论: public string Reverse(string text) { char[] cArray = text.ToCharArray(); string reverse = String.Empty; for (int i = cArray.Length - 1; i > -1; i--) { reverse += cArray[i]; }
public string Reverse(string text)
{
char[] cArray = text.ToCharArray();
string reverse = String.Empty;
for (int i = cArray.Length - 1; i > -1; i--)
{
reverse += cArray[i];
}
return reverse;
}
就我个人而言,我并不热衷于这个功能,我相信有更好的方法来实现它。有吗?看看维基百科条目。它们实现了String.Reverse扩展方法。这允许您编写如下代码:
string s = "olleh";
s.Reverse();
public static string Reverse(this string input)
{
char[] chars = input.ToCharArray();
Array.Reverse(chars);
return new String(chars);
}
他们还使用了这个问题的其他答案所建议的ToCharArray/Reverse组合。源代码如下所示:
string s = "olleh";
s.Reverse();
public static string Reverse(this string input)
{
char[] chars = input.ToCharArray();
Array.Reverse(chars);
return new String(chars);
}
首先,您不需要调用
ToCharArray
,因为字符串已经可以作为char数组进行索引,因此这将为您节省分配
下一个优化是使用StringBuilder
来防止不必要的分配(因为字符串是不可变的,每次连接它们都会生成字符串的副本)。为了进一步优化这一点,我们预先设置了StringBuilder
的长度,因此它不需要扩展其缓冲区
public string Reverse(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
StringBuilder builder = new StringBuilder(text.Length);
for (int i = text.Length - 1; i >= 0; i--)
{
builder.Append(text[i]);
}
return builder.ToString();
}
编辑:性能数据
我使用Array.Reverse
和以下简单程序测试了此函数和函数,其中Reverse1
是一个函数,而Reverse2
是另一个函数:
static void Main(string[] args)
{
var text = "abcdefghijklmnopqrstuvwxyz";
// pre-jit
text = Reverse1(text);
text = Reverse2(text);
// test
var timer1 = Stopwatch.StartNew();
for (var i = 0; i < 10000000; i++)
{
text = Reverse1(text);
}
timer1.Stop();
Console.WriteLine("First: {0}", timer1.ElapsedMilliseconds);
var timer2 = Stopwatch.StartNew();
for (var i = 0; i < 10000000; i++)
{
text = Reverse2(text);
}
timer2.Stop();
Console.WriteLine("Second: {0}", timer2.ElapsedMilliseconds);
Console.ReadLine();
}
static void Main(字符串[]args)
{
var text=“abcdefghijklmnopqrstuvxyz”;
//预jit
text=Reverse1(text);
text=Reverse2(text);
//试验
var timer1=Stopwatch.StartNew();
对于(变量i=0;i<10000000;i++)
{
text=Reverse1(text);
}
timer1.Stop();
WriteLine(“第一个:{0}”,timer1.elapsedmillesons);
var timer2=Stopwatch.StartNew();
对于(变量i=0;i<10000000;i++)
{
text=Reverse2(text);
}
timer2.Stop();
WriteLine(“秒:{0}”,timer2.elapsedmillyses);
Console.ReadLine();
}
结果表明,对于短字符串,
Array.Reverse
方法的速度大约是上述方法的两倍,而对于长字符串,这种差异更为明显。因此,鉴于Array.Reverse
方法既简单又快速,我建议您使用该方法,而不是使用此方法。我把这个放在这里,只是想说明这不是你应该做的事情(让我很惊讶!)这是一个令人惊讶的棘手问题
我建议在大多数情况下使用Array.Reverse,因为它是本机编码的,维护和理解起来非常简单
在我测试的所有案例中,它似乎都优于StringBuilder
public string Reverse(string text)
{
if (text == null) return null;
// this was posted by petebob as well
char[] array = text.ToCharArray();
Array.Reverse(array);
return new String(array);
}
还有第二种方法,对于特定的字符串长度,它可以更快
对于短字符串,Xor似乎可以更快 “更好的方式”取决于在您的情况、性能、优雅度、可维护性等方面,什么对您更重要
无论如何,这里有一种使用数组的方法。相反:
string inputString="The quick brown fox jumps over the lazy dog.";
char[] charArray = inputString.ToCharArray();
Array.Reverse(charArray);
string reversed = new string(charArray);
尝试使用数组。反转
public string Reverse(string str)
{
char[] array = str.ToCharArray();
Array.Reverse(array);
return new string(array);
}
必须提交一个递归示例:
private static string Reverse(string str)
{
if (str.IsNullOrEmpty(str) || str.Length == 1)
return str;
else
return str[str.Length - 1] + Reverse(str.Substring(0, str.Length - 1));
}
抱歉发了这么长的帖子,但这可能会很有趣
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static string ReverseUsingArrayClass(string text)
{
char[] chars = text.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
public static string ReverseUsingCharacterBuffer(string text)
{
char[] charArray = new char[text.Length];
int inputStrLength = text.Length - 1;
for (int idx = 0; idx <= inputStrLength; idx++)
{
charArray[idx] = text[inputStrLength - idx];
}
return new string(charArray);
}
public static string ReverseUsingStringBuilder(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
StringBuilder builder = new StringBuilder(text.Length);
for (int i = text.Length - 1; i >= 0; i--)
{
builder.Append(text[i]);
}
return builder.ToString();
}
private static string ReverseUsingStack(string input)
{
Stack<char> resultStack = new Stack<char>();
foreach (char c in input)
{
resultStack.Push(c);
}
StringBuilder sb = new StringBuilder();
while (resultStack.Count > 0)
{
sb.Append(resultStack.Pop());
}
return sb.ToString();
}
public static string ReverseUsingXOR(string text)
{
char[] charArray = text.ToCharArray();
int length = text.Length - 1;
for (int i = 0; i < length; i++, length--)
{
charArray[i] ^= charArray[length];
charArray[length] ^= charArray[i];
charArray[i] ^= charArray[length];
}
return new string(charArray);
}
static void Main(string[] args)
{
string testString = string.Join(";", new string[] {
new string('a', 100),
new string('b', 101),
new string('c', 102),
new string('d', 103),
});
int cycleCount = 100000;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingCharacterBuffer(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingCharacterBuffer: " + stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingArrayClass(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingArrayClass: " + stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingStringBuilder(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingStringBuilder: " + stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingStack(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingStack: " + stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingXOR(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingXOR: " + stopwatch.ElapsedMilliseconds + "ms");
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
公共静态字符串ReverseUsingArrayClass(字符串文本)
{
char[]chars=text.ToCharArray();
数组。反向(字符);
返回新字符串(字符);
}
公共静态字符串反转CharacterBuffer(字符串文本)
{
char[]charArray=新字符[text.Length];
int inputStrLength=text.Length-1;
对于(int idx=0;idx=0;i--)
{
builder.Append(文本[i]);
}
返回builder.ToString();
}
私有静态字符串反转堆栈(字符串输入)
{
堆栈结果堆栈=新堆栈();
foreach(输入中的字符c)
{
结果钉压(c);
}
StringBuilder sb=新的StringBuilder();
而(resultStack.Count>0)
{
sb.Append(resultStack.Pop());
}
使某人返回字符串();
}
公共静态字符串反转异或(字符串文本)
{
char[]charArray=text.ToCharArray();
int length=text.length-1;
对于(int i=0;iprivate static string Reverse(string str)
{
if (str.IsNullOrEmpty(str) || str.Length == 1)
return str;
else
return str[str.Length - 1] + Reverse(str.Substring(0, str.Length - 1));
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static string ReverseUsingArrayClass(string text)
{
char[] chars = text.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
public static string ReverseUsingCharacterBuffer(string text)
{
char[] charArray = new char[text.Length];
int inputStrLength = text.Length - 1;
for (int idx = 0; idx <= inputStrLength; idx++)
{
charArray[idx] = text[inputStrLength - idx];
}
return new string(charArray);
}
public static string ReverseUsingStringBuilder(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
StringBuilder builder = new StringBuilder(text.Length);
for (int i = text.Length - 1; i >= 0; i--)
{
builder.Append(text[i]);
}
return builder.ToString();
}
private static string ReverseUsingStack(string input)
{
Stack<char> resultStack = new Stack<char>();
foreach (char c in input)
{
resultStack.Push(c);
}
StringBuilder sb = new StringBuilder();
while (resultStack.Count > 0)
{
sb.Append(resultStack.Pop());
}
return sb.ToString();
}
public static string ReverseUsingXOR(string text)
{
char[] charArray = text.ToCharArray();
int length = text.Length - 1;
for (int i = 0; i < length; i++, length--)
{
charArray[i] ^= charArray[length];
charArray[length] ^= charArray[i];
charArray[i] ^= charArray[length];
}
return new string(charArray);
}
static void Main(string[] args)
{
string testString = string.Join(";", new string[] {
new string('a', 100),
new string('b', 101),
new string('c', 102),
new string('d', 103),
});
int cycleCount = 100000;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingCharacterBuffer(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingCharacterBuffer: " + stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingArrayClass(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingArrayClass: " + stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingStringBuilder(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingStringBuilder: " + stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingStack(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingStack: " + stopwatch.ElapsedMilliseconds + "ms");
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < cycleCount; i++)
{
ReverseUsingXOR(testString);
}
stopwatch.Stop();
Console.WriteLine("ReverseUsingXOR: " + stopwatch.ElapsedMilliseconds + "ms");
}
}
}
public static unsafe string Reverse(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
fixed (char* pText = text)
{
char* pStart = pText;
char* pEnd = pText + text.Length - 1;
for (int i = text.Length / 2; i >= 0; i--)
{
char temp = *pStart;
*pStart++ = *pEnd;
*pEnd-- = temp;
}
return text;
}
}
public static string Reverse(this string input)
{
if (input == null)
throw new ArgumentNullException("input");
// allocate a buffer to hold the output
char[] output = new char[input.Length];
for (int outputIndex = 0, inputIndex = input.Length - 1; outputIndex < input.Length; outputIndex++, inputIndex--)
{
// check for surrogate pair
if (input[inputIndex] >= 0xDC00 && input[inputIndex] <= 0xDFFF &&
inputIndex > 0 && input[inputIndex - 1] >= 0xD800 && input[inputIndex - 1] <= 0xDBFF)
{
// preserve the order of the surrogate pair code units
output[outputIndex + 1] = input[inputIndex];
output[outputIndex] = input[inputIndex - 1];
outputIndex++;
inputIndex--;
}
else
{
output[outputIndex] = input[inputIndex];
}
}
return new string(output);
}
public static unsafe string Reverse(string text)
{
int len = text.Length;
// Why allocate a char[] array on the heap when you won't use it
// outside of this method? Use the stack.
char* reversed = stackalloc char[len];
// Avoid bounds-checking performance penalties.
fixed (char* str = text)
{
int i = 0;
int j = i + len - 1;
while (i < len)
{
reversed[i++] = str[j--];
}
}
// Need to use this overload for the System.String constructor
// as providing just the char* pointer could result in garbage
// at the end of the string (no guarantee of null terminator).
return new string(reversed, 0, len);
}
private string Reverse(string stringToReverse)
{
char[] rev = stringToReverse.Reverse().ToArray();
return new string(rev);
}
public string Reverse(string text)
{
return Microsoft.VisualBasic.Strings.StrReverse(text);
}
public static string ReverseString(string stringToReverse)
{
char[] charArray = stringToReverse.ToCharArray();
int len = charArray.Length-1;
int mid = len / 2;
for (int i = 0; i < mid; i++)
{
char tmp = charArray[i];
charArray[i] = charArray[len - i];
charArray[len - i] = tmp;
}
return new string(charArray);
}
public static string Reverse2(string x)
{
char[] charArray = new char[x.Length];
int len = x.Length - 1;
for (int i = 0; i <= len; i++)
charArray[i] = x[len - i];
return new string(charArray);
}
public string Reverse(string input)
{
char[] output = new char[input.Length];
int forwards = 0;
int backwards = input.Length - 1;
do
{
output[forwards] = input[backwards];
output[backwards] = input[forwards];
}while(++forwards <= --backwards);
return new String(output);
}
public string DotNetReverse(string input)
{
char[] toReverse = input.ToCharArray();
Array.Reverse(toReverse);
return new String(toReverse);
}
public string NaiveReverse(string input)
{
char[] outputArray = new char[input.Length];
for (int i = 0; i < input.Length; i++)
{
outputArray[i] = input[input.Length - 1 - i];
}
return new String(outputArray);
}
public string RecursiveReverse(string input)
{
return RecursiveReverseHelper(input, 0, input.Length - 1);
}
public string RecursiveReverseHelper(string input, int startIndex , int endIndex)
{
if (startIndex == endIndex)
{
return "" + input[startIndex];
}
if (endIndex - startIndex == 1)
{
return "" + input[endIndex] + input[startIndex];
}
return input[endIndex] + RecursiveReverseHelper(input, startIndex + 1, endIndex - 1) + input[startIndex];
}
void Main()
{
int[] sizes = new int[] { 10, 100, 1000, 10000 };
for(int sizeIndex = 0; sizeIndex < sizes.Length; sizeIndex++)
{
string holaMundo = "";
for(int i = 0; i < sizes[sizeIndex]; i+= 5)
{
holaMundo += "ABCDE";
}
string.Format("\n**** For size: {0} ****\n", sizes[sizeIndex]).Dump();
string odnuMaloh = DotNetReverse(holaMundo);
var stopWatch = Stopwatch.StartNew();
string result = NaiveReverse(holaMundo);
("Naive Ticks: " + stopWatch.ElapsedTicks).Dump();
stopWatch.Restart();
result = Reverse(holaMundo);
("Efficient linear Ticks: " + stopWatch.ElapsedTicks).Dump();
stopWatch.Restart();
result = RecursiveReverse(holaMundo);
("Recursive Ticks: " + stopWatch.ElapsedTicks).Dump();
stopWatch.Restart();
result = DotNetReverse(holaMundo);
("DotNet Reverse Ticks: " + stopWatch.ElapsedTicks).Dump();
}
}
Naive Ticks: 1
Efficient linear Ticks: 0
Recursive Ticks: 2
DotNet Reverse Ticks: 1
Naive Ticks: 2
Efficient linear Ticks: 1
Recursive Ticks: 12
DotNet Reverse Ticks: 1
Naive Ticks: 5
Efficient linear Ticks: 2
Recursive Ticks: 358
DotNet Reverse Ticks: 9
Naive Ticks: 32
Efficient linear Ticks: 28
Recursive Ticks: 84808
DotNet Reverse Ticks: 33
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
public static class Test
{
private static IEnumerable<string> GraphemeClusters(this string s) {
var enumerator = StringInfo.GetTextElementEnumerator(s);
while(enumerator.MoveNext()) {
yield return (string)enumerator.Current;
}
}
private static string ReverseGraphemeClusters(this string s) {
return string.Join("", s.GraphemeClusters().Reverse().ToArray());
}
public static void Main()
{
var s = "Les Mise\u0301rables";
var r = s.ReverseGraphemeClusters();
Console.WriteLine(r);
}
}
string s = "Blah";
s = new string(s.ToCharArray().Reverse().ToArray());
public string ReverseString(string srtVarable)
{
return new string(srtVarable.Reverse().ToArray());
}
public static string Reverse(string input)
{
return string.Concat(Enumerable.Reverse(input));
}
public static class StringExtensions
{
public static string Reverse(this string input)
{
return string.Concat(Enumerable.Reverse(input));
}
}
public static string Reverse(string text)
{
var stack = new Stack<char>(text);
var array = new char[stack.Count];
int i = 0;
while (stack.Count != 0)
{
array[i++] = stack.Pop();
}
return new string(array);
}
public static string Reverse(string text)
{
var stack = new Stack<char>(text);
return string.Join("", stack);
}
public static string ASCIIReverse(string s)
{
byte[] reversed = new byte[s.Length];
int k = 0;
for (int i = s.Length - 1; i >= 0; i--)
{
reversed[k++] = (byte)s[i];
}
return Encoding.ASCII.GetString(reversed);
}
public static string StrReverse(this string expression)
{
if ((expression == null))
return "";
int srcIndex;
var length = expression.Length;
if (length == 0)
return "";
//CONSIDER: Get System.String to add a surrogate aware Reverse method
//Detect if there are any graphemes that need special handling
for (srcIndex = 0; srcIndex <= length - 1; srcIndex++)
{
var ch = expression[srcIndex];
var uc = char.GetUnicodeCategory(ch);
if (uc == UnicodeCategory.Surrogate || uc == UnicodeCategory.NonSpacingMark || uc == UnicodeCategory.SpacingCombiningMark || uc == UnicodeCategory.EnclosingMark)
{
//Need to use special handling
return InternalStrReverse(expression, srcIndex, length);
}
}
var chars = expression.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
///<remarks>This routine handles reversing Strings containing graphemes
/// GRAPHEME: a text element that is displayed as a single character</remarks>
private static string InternalStrReverse(string expression, int srcIndex, int length)
{
//This code can only be hit one time
var sb = new StringBuilder(length) { Length = length };
var textEnum = StringInfo.GetTextElementEnumerator(expression, srcIndex);
//Init enumerator position
if (!textEnum.MoveNext())
{
return "";
}
var lastSrcIndex = 0;
var destIndex = length - 1;
//Copy up the first surrogate found
while (lastSrcIndex < srcIndex)
{
sb[destIndex] = expression[lastSrcIndex];
destIndex -= 1;
lastSrcIndex += 1;
}
//Now iterate through the text elements and copy them to the reversed string
var nextSrcIndex = textEnum.ElementIndex;
while (destIndex >= 0)
{
srcIndex = nextSrcIndex;
//Move to next element
nextSrcIndex = (textEnum.MoveNext()) ? textEnum.ElementIndex : length;
lastSrcIndex = nextSrcIndex - 1;
while (lastSrcIndex >= srcIndex)
{
sb[destIndex] = expression[lastSrcIndex];
destIndex -= 1;
lastSrcIndex -= 1;
}
}
return sb.ToString();
}
char[] chars = new char[str.Length];
for (int i = str.Length - 1, j = 0; i >= 0; --i, ++j)
{
chars[j] = str[i];
}
str = new String(chars);
static class ExtentionMethodCollection
{
public static string Inverse(this string @base)
{
return new string(@base.Reverse().ToArray());
}
}
string Answer = "12345".Inverse(); // = "54321"
static String Reverse2(string str)
{
int strLen = str.Length, elem = strLen - 1;
char[] charA = new char[strLen];
for (int i = 0; i < strLen; i++)
{
charA[elem] = str[i];
elem--;
}
return new String(charA);
}
static String Reverse(string str)
{
char[] charA = str.ToCharArray();
Array.Reverse(charA);
return new String(charA);
}
public static string reverse(string s)
{
string r = "";
for (int i = s.Length; i > 0; i--) r += s[i - 1];
return r;
}
string s = "z̽a̎l͘g̈o̓Simplest way:
string reversed = new string(text.Reverse().ToArray());
| Method | InputLength | Mean | Error | StdDev | Gen 0 | Allocated |
| ---------------- | ----------- | -----------: | ---------: | --------: | -----: | --------: |
| WithReverseArray | 10 | 45.464 ns | 0.4836 ns | 0.4524 ns | 0.0610 | 96 B |
| WithStringCreate | 10 | 39.749 ns | 0.3206 ns | 0.2842 ns | 0.0305 | 48 B |
| | | | | | | |
| WithReverseArray | 100 | 175.162 ns | 2.8766 ns | 2.2458 ns | 0.2897 | 456 B |
| WithStringCreate | 100 | 125.284 ns | 2.4657 ns | 2.0590 ns | 0.1473 | 232 B |
| | | | | | | |
| WithReverseArray | 1000 | 1,523.544 ns | 9.8808 ns | 8.7591 ns | 2.5768 | 4056 B |
| WithStringCreate | 1000 | 1,078.957 ns | 10.2948 ns | 9.6298 ns | 1.2894 | 2032 B |