string strContent = @"That is a hat"; string strVlaue = ""; Regex re = new Regex(@"[^\s]+"); foreach (Match m in re.Matches(strContent)) { strVlaue = m.Value.Trim() + " " + strVlaue; }取strVlaue 即可
很久以前写的了......不是很好.. int count = 0; int begin = 0; string txt = "I am a programmer"; Console.WriteLine(txt); Console.WriteLine("反转之后"); for (int i = txt.Length - 1; i >= 0; i--) { if (i == txt.Length - 1) count = i; if (i == 0) begin = 0; else begin = i + 1; if (txt[i] == ' ' || i == 0) { for (int j = begin; j <= count; j++) Console.Write(txt[j]); if (i != 0) Console.Write(" "); count = i - 1; } } Console.Read();
string strValue = "That is a hat"; string[] arrValue = strValue.Split (new char[] { ' ' }); Array.Reverse (arrValue); strValue = string.Join (" ", arrValue);
public void test() { //字符串:That is a hat //反转成:hat is That string a = "That is a hat"; string[] b = a.Split(' '); string c = string.Empty; for (int i = b.Length-1; i >= 0; i--) { c += b[i]; } //输出c的值。 }
public string Reverse(string str) { string newstr = ""; string[] list= str.Split(' '); for (int i = list.Length; i >0; i--) { newstr += list[i - 1]+" "; } newstr = newstr.Substring(0, newstr.Length - 1); return newstr; }
string str = "That is a hat"; string[] strArray = str.Split(' '); Array.Reverse(strArray);
将List<String>反转,
遍历List<String>拼接字符串,得到结果
string strContent = @"That is a hat";
string strVlaue = "";
Regex re = new Regex(@"[^\s]+");
foreach (Match m in re.Matches(strContent))
{
strVlaue = m.Value.Trim() + " " + strVlaue;
}取strVlaue 即可
很久以前写的了......不是很好.. int count = 0;
int begin = 0;
string txt = "I am a programmer";
Console.WriteLine(txt);
Console.WriteLine("反转之后");
for (int i = txt.Length - 1; i >= 0; i--)
{
if (i == txt.Length - 1)
count = i;
if (i == 0)
begin = 0;
else
begin = i + 1;
if (txt[i] == ' ' || i == 0)
{
for (int j = begin; j <= count; j++)
Console.Write(txt[j]);
if (i != 0)
Console.Write(" ");
count = i - 1;
}
}
Console.Read();
string strValue = "That is a hat";
string[] arrValue = strValue.Split (new char[] { ' ' });
Array.Reverse (arrValue);
strValue = string.Join (" ", arrValue);
{
//字符串:That is a hat
//反转成:hat is That
string a = "That is a hat";
string[] b = a.Split(' ');
string c = string.Empty;
for (int i = b.Length-1; i >= 0; i--)
{
c += b[i];
}
//输出c的值。
}
public string Reverse(string str)
{
string newstr = "";
string[] list= str.Split(' ');
for (int i = list.Length; i >0; i--)
{
newstr += list[i - 1]+" ";
}
newstr = newstr.Substring(0, newstr.Length - 1);
return newstr;
}
string[] strArray = str.Split(' ');
Array.Reverse(strArray);
string resultStr = string.Join(" ", strArray);