简单的方法来扭转每个单词在句子 [英] Easy way to reverse each word in a sentence
问题描述
例如:
字符串str =我要扭转我自己。
串strrev =我是个gniog OT esrever .flesym //一个简单的方法来实现这一目标
当我觉得我必须通过每一个字,然后将每个迭代每一个字的信。
我所做的工作正常。 但是我需要轻松/一小段路
C#代码:
字符串str =我要扭转我自己。
串strrev =;
的foreach(在str.Split('')VAR字)
{
字符串TEMP =;
的foreach(在word.ToCharArray变种CH())
{
TEMP = CH +温度;
}
strrev = strrev +温度+;
}
Console.WriteLine(strrev); //我是个gniog OT esrever .flesym
好了,这里有一个LINQ的解决方案:
VAR reversedWords =的string.join(,
str.Split('')
。选择(X =>新建字符串(x.Reverse()ToArray的())));
如果您正在使用.NET 3.5,你需要相反的顺序转换为数组太:
VAR reversedWords =的string.join(,
str.Split('')
。选择(X =方式>新的String(x.Reverse()ToArray的()))
.ToArray());
在换句话说:
- $ B在空间$ b
- 拆分
- 对于每一个字,通过将输入的字符序列创造了一个新词,扭转这种顺序,把结果到一个数组,然后调用
字符串(CHAR [])
构造 - 根据框架版本,通话
ToArray的()
的字符串序列,如.NET 4有可用 - 呼叫
的string.join
对结果更重载把颠倒的话再聚首。
请注意,倒车字符串的方式是有点麻烦。它很容易地创建一个扩展方法做到这一点:
//不要只是把它作为反向否则它与冲突LINQ版本。
公共静态字符串ReverseText(此字符串文本)
{
的char [] =字符text.ToCharArray();
Array.Reverse(字符);
返回新的字符串(字符);
}
请注意,这是的还是的错误,在不同的方式 - 它不会与组合字符,代理对等,这只是反转的UTF-16编码单元原字符串中的顺序处理。罚款玩耍,但是你要明白它为什么不使用它的真实数据是个好主意。
Example:
string str = "I am going to reverse myself.";
string strrev = "I ma gniog ot esrever .flesym"; //An easy way to achieve this
As I think I have to iterate through each word and then each letter of every word.
What I have done works fine. But I need easy/short way.
C# CODE:
string str = "I am going to reverse myself.";
string strrev = "";
foreach (var word in str.Split(' '))
{
string temp = "";
foreach (var ch in word.ToCharArray())
{
temp = ch + temp;
}
strrev = strrev + temp + "";
}
Console.WriteLine(strrev); //I ma gniog ot esrever .flesym
Well, here's a LINQ solution:
var reversedWords = string.Join(" ",
str.Split(' ')
.Select(x => new String(x.Reverse().ToArray())));
If you're using .NET 3.5, you'll need to convert the reversed sequence to an array too:
var reversedWords = string.Join(" ",
str.Split(' ')
.Select(x => new String(x.Reverse().ToArray()))
.ToArray());
In other words:
- Split on spaces
- For each word, create a new word by treating the input as a sequence of characters, reverse that sequence, turn the result into an array, and then call the
string(char[])
constructor - Depending on framework version, call
ToArray()
on the string sequence, as .NET 4 has more overloads available - Call
string.Join
on the result to put the reversed words back together again.
Note that this way of reversing a string is somewhat cumbersome. It's easy to create an extension method to do it:
// Don't just call it Reverse as otherwise it conflicts with the LINQ version.
public static string ReverseText(this string text)
{
char[] chars = text.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
Note that this is still "wrong" in various ways - it doesn't cope with combining characters, surrogate pairs etc. It simply reverses the sequence of UTF-16 code units within the original string. Fine for playing around, but you need to understand why it's not a good idea to use it for real data.
这篇关于简单的方法来扭转每个单词在句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!