1、关于将字符串翻转的最简单的解决办法是:将string转成char数组,在调用数组的Reverse(a)方法,最后遍历输出就可以了。参考代码如下: 方法1、 string chars = “luzhishen” char[] a = chars.ToCharArray(); Array.Reverse(a); foreach (char c in a) { Console.Write("{0}",c); }
方法2: string str = "abc";
char[] newstr = str.ToCharArray(); char temp = newstr[0];
for (int i = 0; i < newstr.Length / 2; i++)
{
temp = newstr;
newstr = newstr[newstr.Length - 1 - i];
newstr[newstr.Length - 1 - i] = temp;
}
for (int i = 0; i < newstr.Length; i++)
{
Console.WriteLine(newstr);
}
2、关于将多余符号删除
string temp = "";
string str = "123-456---789-----123-2";
string[] strs = str.Split(new char[]{'-'},StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < strs.Length-1 ; i++)
{
temp += strs + '-';
}
Console.WriteLine(temp+strs[strs.Length-1]);
3、关于提取用户名和域名所用的string方法
string email = "abc@163.com";
int num = email.IndexOf('@');
string name = email.Substring(0, num);
string dns = email.Substring(num + 1);
Console.WriteLine("用户名是{0},域名是{1}", name, dns);
好友不明白的请回帖
|