C#中使用Split方法分割字符串和Replaced的使用

简单的描述:

string a = "a,b,c";
string[] arr = a.Split(',');//根据逗号分割成字符串数组,数组中的内容是a、b和c三个值。
string s = a.Replace(',', ';'); //这个是用分号去替换逗号,出来的结果为:a;b;c

具体的描述:

1)用指定的单字符分割字符串

源码示例:

using System;
using System.Collections.Generic;
using System.Text;

namespace f1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = "abcdeabcdeabcde";
            string[] sa1 = s1.Split('c'); //指定分隔符'c’
            foreach(string i in sa1)
                Console.Write(i);         

         }
    }
}

2)用多个指定字符(字符串)分割字符串

源码示例:

using System;
using System.Collections.Generic;
using System.Text;

namespace f2
{
    class Program
    {
        static void Main(string[] args)
        {
            string s2 ="abcdeabcdeabcde";
            string[] sa = s2.Split(new char[3]{'c','d','e'});
            foreach(string i in sa)

                Console.Write(i);
        }
    }
}

3)对多个指定字符进行替换(Replace),然后使用1)中方法

源码示例:

using System;
using System.Collections.Generic;
using System.Text;

namespace f4
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "我*****热*****爱*****祖*****国";
            string[] str2;
            str1=str1.Replace("*****","*");
            str2=str1.Split('*');
            foreach(string i in str2)
                Console.WriteLine(i.ToString()+"---<br>");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值