示例:
string str = "I am going to reverse myself.";
string strrev = "I ma gniog ot esrever .flesym"; //An easy way to achieve this
我认为我必须遍历每个单词,然后是每个单词的每个字母。
我所做的一切都很好。 但我需要轻松/简短的方式。
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
答案 0 :(得分:16)
嗯,这是一个LINQ解决方案:
var reversedWords = string.Join(" ",
str.Split(' ')
.Select(x => new String(x.Reverse().ToArray())));
如果您使用的是.NET 3.5,则还需要将反转序列转换为数组:
var reversedWords = string.Join(" ",
str.Split(' ')
.Select(x => new String(x.Reverse().ToArray()))
.ToArray());
换句话说:
string(char[])
构造函数ToArray()
,因为.NET 4有更多可用的重载string.Join
,将反转的字词重新组合在一起。请注意,这种反转字符串的方式有些麻烦。创建扩展方法很容易:
// 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);
}
请注意,这是仍然“错误”的各种方式 - 它不能处理组合字符,代理对等。它只是反转原始字符串中的UTF-16代码单元序列。适合玩游戏,但你需要理解为什么将它用于真实数据并不是一个好主意。
答案 1 :(得分:5)
要反转我使用的字符串:
new String( word.Reverse().ToArray() )
Reverse()
函数是LINQ的一部分,因为String实现了IEnumerable<char>
。其结果是另一个IEnumerable<char>
,现在需要将其转换为字符串。您可以通过调用ToArray()
来提供char[]
,然后将其传递到string
的构造函数中。
所以完整的代码变成:
string s="AB CD";
string reversed = String.Join(" ",
s.Split(' ')
.Select(word => new String( word.Reverse().ToArray() ) ));
请注意,此代码不适用于某些unicode功能。它至少有两个问题:
char
s。反转它们会破坏编码。这是相对容易修复的,因为只有有限数量的字符启动这样的序列(如果我没记错的话,则为16),这很可能在将来的unicode版本中不会扩展。答案 2 :(得分:0)
你可以使用linq作为
String newStr = new String( str.Reverse().ToArray() );
答案 3 :(得分:0)
1 - 反转字符串的扩展方法
public static string reverseString(this string description) {
char[] array = description.ToCharArray().Reverse().ToArray();
return new string(array);
}
2 - 反转数组并反转此数组的所有字符串
public static string reverseText(this string description) {
string [] reversetext= description.Split(' ').Select(i => i.ToString().reverseString()).Reverse().ToArray();
return string.Join(" ", reversetext);
}
答案 4 :(得分:0)
我使用XOR从此处http://en.wikipedia.org/wiki/XOR_swap_algorithm
进行交换X := X XOR Y
Y := X XOR Y
X := X XOR Y
C#是:
public string ReverseWords(string str)
{
StringBuilder strrev = new StringBuilder();
StringBuilder reversedword = new StringBuilder();
foreach (var word in str.Split(' '))
{
char[] singlesentence = word.ToCharArray();
int j = singlesentence.Length / 2;
if (j > 0)
{
for (int i = singlesentence.Length - 1, c = 0; i == j; c = c + 1, i = i - 1)
{
singlesentence[c] = (char)(singlesentence[c] ^ singlesentence[i]);
singlesentence[i] = (char)(singlesentence[c] ^ singlesentence[i]);
singlesentence[c] = (char)(singlesentence[c] ^ singlesentence[i]);
}
}
strrev.Append(singlesentence);
strrev.Append(" ");
}
return strrev.ToString();
}
答案 5 :(得分:0)
//Without Extension Methods Like: Split, ToCharArray, etc..
public string ReverseString(string str="Hai How Are You?"){
var FullRev="",
var wordRev="";
for(i=0;i<=str.length;i++){
if(str[i]==" " || i==str.length){
FullRev=FullRev+" "+wordRev; //FullRev=wordRev+" "+FullRev;
wordRev="";
}else{
wordRev=str[i]+wordRev;
}
}
return FullRev;
}
//Result "iaH woH erA ?uoY"
答案 6 :(得分:-1)
public static void ReverseEachWordString(string abc)
{
int start_index = 0, end_index = abc.Length - 1;
int i = 0, j = 0;
char[] arr = abc.ToCharArray();
try
{
while (start_index < end_index)
{
if (arr[start_index] == ' ')
{
Console.WriteLine(arr[start_index]);
start_index++;
i = start_index;
}
else
{
if (arr[i] != ' ')
{
if (i == end_index)
{
i++;
for (j = i - 1; j >= start_index; j--)
{
Console.WriteLine(arr[j]);
}
break;
}
else
i++;
}
else
{
for (j = i - 1; j >= start_index; j--)
{
Console.WriteLine(arr[j]);
}
i++;
start_index = i - 1;
}
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
答案 7 :(得分:-2)
static void Main(string[] args)
{
string str = "Hi how are you";
char[] char_arr= str.ToCharArray();
string finalstr = "";
string eachwords = "";
string tempreverseword = "";
int char_length = char_arr.Length;
for (int i = 0; i < char_arr.Length; i++)
{
if (char_arr[i].ToString() == " " || i == char_length-1)
{
if (i == char_length - 1)
{
eachwords += char_arr[i] + "";
}
char[] revchar_arr = eachwords.ToCharArray();
for (int j = revchar_arr.Length-1; j >=0; j--)
{
tempreverseword += revchar_arr[j];
}
finalstr += tempreverseword+" ";
tempreverseword = "";
eachwords = "";
}
else
{
eachwords += char_arr[i] + "";
}
}
Console.WriteLine(finalstr);
Console.ReadKey();
}
答案 8 :(得分:-2)
string str = "I am going to reverse myself.";
string[] strArray = str.Split(' ');
string[] strArrayReversed = new string[strArray.Length];
for(int i = 0; i < strArray.Length; i++)
{
char[] eachWordLetter = strArray[i].ToCharArray();
Array.Reverse(eachWordLetter);
strArrayReversed[i] = new string(eachWordLetter);
}
string newReversedString = String.Join(" ", strArrayReversed);
Console.WriteLine(newReversedString);
Console.ReadLine();