我有一个我需要扭转的字符串 [英] I have a string which I need to reverse

查看:79
本文介绍了我有一个我需要扭转的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个我需要扭转的字符串。我一直在寻找最简单的方法(一如既往!)

所以试过字符串看看是否存在,它不会或者我找不到它。

MSDN有很多建议

 Console.WriteLine( 请输入一个字符串,打印输入结束); 
string input = Console.ReadLine();
StringBuilder sb = new StringBuilder();
int i = input.Length-1;
while (i!= - 1
{
sb .Append(输入[我 - ]);
}
Console.WriteLine( Reverse stirng is + sb.ToString ());

这似乎是一种正确的做法我现在只需要将bb字符串构建器变量设置为等于字符串下一位编辑它需要作为下一位处理的字符串。

如果我做sb = Next(下一个是字符串)我得到错误不能隐式转换类型''字符串''到'' System.Text.StringBuilder'。所以两个问题是正确的方法,如果是这样的话,我怎样才能得到一个字符串,如果不是正确的方法呢?

格伦

解决方案

LINQ可能是您可以考虑的另一种方法。

字符串撤销 [ ^ ]讨论了三种反转字符串的方法。

你试过搜索吗? 这个是c#string reverse的第一个热门,并回答你的问题题。似乎Array.Reverse方法是最好的。


Hi All,

I have a string I need to reverse. I was looking for the simplest way (as always!)
so have tried the string to see if that exists, it doesn''t or I can''t find it.
MSDN have lots of suggestions along the lines of

Console.WriteLine("Pleae enter a string, print enter to end");
string input = Console.ReadLine();
StringBuilder sb = new StringBuilder();
int i = input.Length-1;
while (i != - 1)
{
      sb.Append(input[i--]);
}
Console.WriteLine("Reverse stirng is " + sb.ToString());

which seems to be a the correct way of doing it I just now
need to get the sb the string builder "variable" to equal a string for the next bit of editing it need as a string for the next bit of processing.
if I do sb = Next (Next is a string) I get the Error Cannot implicitly convert type ''string'' to ''System.Text.StringBuilder''. So two questions is this the right way, if so how do I get a string out it, if not what is the right way?
Glenn

解决方案

LINQ could be another approach you could consider.
String Reversal[^] discusses three approaches to reverse a string.


Did you try searching? This was the first hit for ''c# string reverse'' and answers your question. It seems the Array.Reverse approach is the best.


这篇关于我有一个我需要扭转的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆