C#学习:字符串函数详解

一、字符串分割:Split

练习使用Split对字符串进行分割。

Split使用方法(1):string[] strs1 = s1.Split('分割符');

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

namespace 字符串分割
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = "aaa,bbb.ccc\\ddd";
            string[] strs = s1.Split(',','.','\\');
            foreach (string item in strs)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
         }

    }
}

Split可以使用可变参数,即可以规定多个分隔符。(注意转义字符\作为分隔符时的情况)

执行结果:

字符串根据分割符被分割为字符数组打印出来。

另外Split可以重载,能够把字符串中空白字符移除掉:

Split使用方法(2):string[] strs2 = s1.Split(new char {'分割符'},StringSplitOptions.RemoveEmptyEntries);

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

namespace 字符串分割
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = "aaa,bbb.ccc\\ddd,ee,,f";
            string[] strs1 = s1.Split(',','.','\\');
            string[] strs2 = s1.Split(new char[] { ',', '.', '\\' },StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in strs1)
            {
                Console.WriteLine(item);
            }
            foreach (string item in strs2)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
         }

    }
}

执行结果(字符串中的空格被移除):

 

 Split使用方法(3):string[] strs3 = s1.Split(new shring{"字符串分割符"},StringSplitOptions.RemoveEmptyEntries);

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

namespace 字符串分割
{
    class Program
    {
        static void Main(string[] args)
        {

            string s1 = "我是刘德华我是甄子丹我是张学友";
            string[] strs1=s1.Split(new string[]{"我是"},StringSplitOptions.RemoveEmptyEntries);
          
             foreach (string item in strs1)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
         }

    }
}

执行结果(按字符串对字符串进行分割):

 

二、字符串替换:string Replace(string oldValue,string newValue)

将字符串中出现oldValue的地方替换为newValue

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

namespace 字符串替换
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "李雷成绩很好,向李雷同学学习";
            s = s.Replace("李雷", "韩梅梅");
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }
}

执行结果:

三、取子字符串:

1、string Substring(int startIndex),取从位置startIndex开始一直到最后的子字符串

2、string Substring(int startIndex,int length),取从位置startIndex开始长度为length的子字符串,如果子字符串的长度不足length则报错

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

namespace SubString函数
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "http://www.baidu.com";
            string 域名 = s.Substring(7);
            Console.WriteLine(域名);

            string s1 = s.Substring(7, 5);
            Console.WriteLine(s1);
            Console.ReadKey();
        }
    }
}

执行结果:

 四、判断字符串是否含有字串

bool Contains(string value)

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

namespace 包含
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "我们的社会真和谐!";
            if (s.Contains("社会") || s.Contains("和谐"))
            {
                Console.WriteLine("含有敏感词汇");
            }
           Console.ReadKey();
        }
    }
}

执行结果:

其他字符串函数:

bool StartsWith(string value)判断字符串是否以字串value开始

bool EndsWith(string value)判断字符串是否以字串value结束

int IndexOf(string value)取字串value第一次出现的位置,用法如下:

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

namespace 字符串综合练习
{
    class Program
    {
        static void Main(string[] args)
        {
            string email = Console.ReadLine();
            int i=email.IndexOf("@");
            string username = email.Substring(0, i);
            string 域名 = email.Substring(i + 1);
            Console.WriteLine("用户名:" + username);
            Console.WriteLine("域名:" + 域名);
            Console.ReadKey();
        }

    }
}

执行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值