首页 > 编程 > C# > 正文

C#实现字符串倒序的写法

2020-01-24 01:09:49
字体:
来源:转载
供稿:网友

本文实例为大家分享了C#字符串倒序写法的实现代码,供大家参考,具体内容如下

//string concatenation with for looppublic string ReverseA(string text){  char[] cArray = text.ToCharArray();  string reverse = String.Empty;  for (int i = cArray.Length - 1; i > -1; i--)  {    reverse += cArray[i];  }  return reverse;}// Array.Reverse functionpublic string ReverseB(string text){  char[] charArray = text.ToCharArray();  Array.Reverse(charArray);  return new string(charArray);}// push/pop Stack<>public string ReverseC(string text){  Stack resultStack = new Stack();  foreach (char c in text)  {    resultStack.Push(c);  }  StringBuilder sb = new StringBuilder();  while (resultStack.Count > 0)  {    sb.Append(resultStack.Pop());  }  return sb.ToString();}// LINQpublic string ReverseD(string text){  return new string(text.ToCharArray().Reverse().ToArray());}// StringBuilderpublic string ReverseE(string text){  char[] cArray = text.ToCharArray();  StringBuilder reverse = new StringBuilder();  for (int i = cArray.Length - 1; i > -1; i--)  {    reverse.Append(cArray[i]);  }  return reverse.ToString();}

以上就是本文的全部内容,希望对大家学习C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表