C#实现字符串倒序的写法
程序员文章站
2023-12-26 21:44:27
本文实例为大家分享了c#字符串倒序写法的实现代码,供大家参考,具体内容如下
//string concatenation with for loop
publi...
本文实例为大家分享了c#字符串倒序写法的实现代码,供大家参考,具体内容如下
//string concatenation with for loop public 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 function public 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(); } // linq public string reversed(string text) { return new string(text.tochararray().reverse().toarray()); } // stringbuilder public 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#程序设计有所帮助。