private static void TestRegex15() { string s = "Welcome_to__Mircosoft!"; Regex regExp = new Regex(@"[a-zA-Z]+|[!]|_+", RegexOptions.Compiled); MatchCollection mc = regExp.Matches(s); int i = mc.Count; string result = regExp.Replace(s, delegate(Match m) { return mc[--i].Value; }); Console.WriteLine(result); }
Array.Reverse(arr);
return new string(arr);
http://www.cnblogs.com/BpLoveGcy/articles/1692051.html
{
string s = "Welcome_to__Mircosoft!";
Regex regExp = new Regex(@"[a-zA-Z]+|[!]|_+", RegexOptions.Compiled);
MatchCollection mc = regExp.Matches(s);
int i = mc.Count;
string result = regExp.Replace(s, delegate(Match m) { return mc[--i].Value; });
Console.WriteLine(result);
}
{
string returnStr = "";
if(!string.IsNullOrEmpty(str))
{
int count = str.Length;
for(int index=count-1;index>=0 ;index--)
{
returnStr += str[index];
//returnStr+=
}
}
return returnStr;
}
1、用第一个字符和最后一个字符交换,注意临界。
2、定义一个新串,将原串的字符倒着加起来。
在算法和数据结构中常见这类问题。
string[] sp=new string[] { "_","__","!"};
string[] ss = s.Split(sp, StringSplitOptions.RemoveEmptyEntries);
string s2 = "";
for (int i = ss.Length - 1; i >= 0; i--)
{
s2 += sp[i]+ss[i];
}
Response.Write(s2);//!Mircosoft__to_Welcome
//这个很好玩,你可以试试
string s = "Welcome_to__Mircosoft!";
string s1 = new string(s.ToCharArray().Reverse().ToArray());
Response.Write(s1); //!tfosocriM__ot_emocleW
{
string s = "Welcome_to__Mircosoft!";
StringBuilder temp = new StringBuilder();
List<string> sections = new List<string>();
int status = -1;//-1 初始化 0 字母 1 符号 2 下划线
foreach (char c in s)
{
int current_status = -1;
if (char.IsLetter(c))
{
current_status = 0;
}
else if (c == '_')
{
current_status = 2;
}
else
{
current_status = 1;
}
if (current_status != status)
{
status = current_status;
if (temp.Length > 0) sections.Insert(0, temp.ToString());
temp.Remove(0, temp.Length);
}
temp.Append(c);
}
foreach (string section in sections)
{
temp.Append(section);
}
Console.WriteLine(temp.ToString());
}输出:
!Mircosoft__to_Welcome
public void ReturnStr(string str)
{
string tmp="",result="";
for(int i = str.Length-1;i>-1;i--)
{
char a = str[i];
if(i == str.Length-1)
{
if(a=='!')
{
result = a.ToString();
}
if(a=='_')
{
result += tmp + '_';
tmp='_';
}
if(a=='!')
{
result+=tmp;
tmp="";
}
else{
tmp = str[i]+tmp
response.write(result);
}
}
}
}
我把整个串拆分成若干个char型,然后判断空格,再进行标点字符的判断上,我限制了代码的扩展
string s = "Welcome_to__Mircosoft!";
string[] sp=new string[] { "_","__","!"};
string[] ss = s.Split(sp, StringSplitOptions.RemoveEmptyEntries);
string s2 = "";
for (int i = ss.Length - 1; i >= 0; i--)
{
s2 += sp[i]+ss[i];
}
Response.Write(s2);//!Mircosoft__to_Welcome
//这个很好玩,你可以试试
string s1 = new string(s.ToCharArray().Reverse().ToArray());
Response.Write(s1); //!tfosocriM__ot_emocleW
{
string s = "Welcome_to__Mircosoft!";
string temp = "";
string result = "";
int status = -1;//-1 初始化 0 字母 1 符号 2 下划线
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
int current_status = -1;
if ((c >= 'a' && c <= 'z') | (c >= 'A' && c <= 'Z'))
{
current_status = 0;
}
else if (c == '_')
{
current_status = 2;
}
else
{
current_status = 1;
}
if (current_status != status)
{
status = current_status;
if (temp.Length > 0) result = temp + result;
temp = "";
}
temp += new string(c, 1);
}
result = temp + result;
Console.WriteLine(result);
}
这下可以了?
这算是使用库函数了吧? Reverse()string returnString = "";
char[] array = 字符串.ToCharArray();
for(int i=array.length-1; i>=0; i--)
{
returnString += array[i]
}return retrunString;
{
string str = "Welcome_to__Mircosoft!";
char[] ch1 = str.ToCharArray();
str = "";
for (int i = ch1.Length - 1; i >= 0; i--)
{
str += ch1[i].ToString();
}
}
你不可用 Console.WriteLine(result); 输出
结贴了,28可用,我也做出一个新的方法,扩展性也不错,你们可以参考一下。string reverseString(string inStr)
{
string returnStr = "";
string tmp = inStr[0].ToString();
bool preChar = isChar(inStr[0]);
for (int i = 1; i < inStr.Length; i++)
{
char a = inStr[i];
bool b = isChar(a);
if (b)
{
if (preChar)
tmp += a.ToString();
else
{
returnStr = tmp + returnStr;
tmp = a.ToString();
if(i==inStr.Length-1)
returnStr = tmp + returnStr;
}
}
else
{
if (preChar)
{
returnStr = tmp + returnStr;
tmp = a.ToString();
if (i == inStr.Length - 1)
returnStr = tmp + returnStr;
}
else
{
tmp += a.ToString();
}
}
preChar = b;
}
return returnStr;
} bool isChar(char inChar)
{
if (((int)inChar >= 65 && (int)inChar <= 90) || ((int)inChar >= 97 && (int)inChar <= 122))
return true;
else return false;
}
!tfosocriM__ot_emocleW第二次,碰到_就反转前面的部分!tfosocriM__ot_emocleW =>
!Mircosoft__to_Welcome这样空间是O(1)的
{
if(!pOut) return NULL;
iOutLen = iInLen; for(int i=0; i<iInLen; i++)
{
pOut[iInLen-i-1] = pSource[i];
} return pOut;
}
{
Antion();
}
static void Antion()
{
string s = "Welcome_to__Mircosoft!";
for (int i = s.Length - 1; i >= 0; i--)
{
Console.WriteLine(s[i]);
}
}