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.ReadKey();

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();

emal提取:abc@163.com -> abc 163.com

string email = "abc@163.com";
int index = email.IndexOf('@');
string userName = email.Substring(0, index);
string yuming = email.Substring(index + 1);
Console.WriteLine(userName);
Console.WriteLine(yuming);
Console.ReadKey();

找 e 的位置

String str = "abcdefgegegegegeg";
int index = str.IndexOf('e');
Console.WriteLine("第1次出现e的位置{0}", index);
int count = 1;
while (index != -1)
{
    count++;
    index = str.IndexOf('e', index + 1);
    if (index == -1)
    {
        break;
    }
    Console.WriteLine("第{0}次出现e的位置{1}", count, index);
}
Console.ReadKey();

老牛很邪恶 -> 老牛很**

string str = "老牛很邪恶";
if (str.Contains("邪恶"))
{
    str = str.Replace("邪恶", "**");
}
Console.WriteLine(str);
Console.ReadKey();

{ "诸葛亮", "鸟叔", "卡卡西" } 变成 诸葛亮|鸟叔|卡卡西。2、把 “|” 去掉。

string[] names = { "诸葛亮", "鸟叔", "卡卡西" };
string str = string.Join("|", names);
string[] strNew = str.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
str = string.Join("", strNew);
Console.WriteLine(str);
Console.ReadKey();

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值