C#字符串的处理

abcde ---------> edcba

string str = "abcde";
char[] chs = str.ToCharArray();
for (int i = 0; i < chs.Length/2; i++)
{
char temp = chs[i];
chs[i] = chs[chs.Length - 1 - i];
chs[chs.Length - 1 - i] = temp;
}
str = new string(chs);
Console.WriteLine(str);
Console.ReadLine();

hello c sharp ------------> sharp c hello

string str = "hello c sharp";
string[] strnew = str.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < strnew.Length/2; i++)
{
    string temp = strnew[i];
    strnew[i] = strnew[strnew.Length - 1 - i];
    strnew[strnew.Length - 1 - i] = temp;
}
str = string.Join(" ", strnew);
Console.WriteLine(str);
Console.ReadKey();

[email protected] 提取名称和域名

 string email = "[email protected]";
 int index = email.IndexOf("@");
 string username = email.Substring(0,index);
 string yuming = email.Substring(index + 1);
 Console.WriteLine(username);
 Console.WriteLine(yuming);
 Console.ReadKey();

查找?出现的位置

string str = "asjlkfjaslkfjlksjfajfla";
int index = str.IndexOf('a');
Console.WriteLine("第{0}次出现a的位置", index);
for (int i = 0; i < str.Length; i++)
{
    index = str.IndexOf('a',index + 1);
    if (index == -1)
    {
        break;
    }
    Console.WriteLine("第{0}次出现a的位置", index);
}

敏感词汇过滤

string str = "秋名山老司机";
if (str.Contains("老司机"))
{
str.Replace("老司机", "***");
}
Console.WriteLine(str);
Console.ReadKey();

你可能感兴趣的:(c#)