C# 之 字符串截取--Substring

33 篇文章 10 订阅
       说到字符串截取,大家首先相当的应该就是substring函数,今天就来给大家讲讲substring函数。


1.public String Substring(int startIndex);

       从此字符串检索子字符串。 子字符串从指定的字符位置(第startIndex个字符)开始,一直到此字符串末尾。

class Program
    {
        static void Main(string[] args)
        {
            string s1 = "所属机构名称/教师姓名/课程类型/课程名称";

            Console.WriteLine(s1.Substring(0));
            Console.WriteLine(s1.Substring(4));
            Console.WriteLine(s1.Substring(s1.Length));
            Console.WriteLine("--------------------------------");
            Console.WriteLine(s1.Substring(s1.Length+1));
            
        }
    }




2.public string Substring(int startIndex, int length);
        从此字符串检索子字符串。 子字符串从指定的字符位置第startIndex个字符)开始,且具有指定的长度(子字符串的长度length)。

class Program
    {
        static void Main(string[] args)
        {
            string s1 = "所属机构名称/教师姓名/课程类型/课程名称";

            Console.WriteLine(s1.Substring(0,0));
            Console.WriteLine(s1.Substring(4,10));
            Console.WriteLine(s1.Substring(s1.Length,0));
            Console.WriteLine("--------------------------------");
            //Console.WriteLine(s1.Substring(4,s1.Length));     //字符串长度超出范围
            //Console.WriteLine(s1.Substring(s1.Length,1));     //字符串长度超出范围
            Console.WriteLine(s1.Substring(s1.Length+1));       //开始位置不能大于字符串长度
            
        }
    }





        现在我们有这么一个需求,将"所属机构名称/教师姓名/课程类型/课程名称"这个字符串,按照"/"分别截取出来,下面看看我们用Substring函数怎么实现。

class Program
    {
        static void Main(string[] args)
        {
            string s1 = "所属机构名称/教师姓名/课程类型/课程名称";

            int first = s1.IndexOf("/")+1;      //第一个"/"的位置
            int second = s1.IndexOf("/", first + 1) + 1;      //第二个"/"的位置
            int third = s1.IndexOf("/", second + 1) + 1;       //第三个"/"的位置
            Console.WriteLine("第一个'/ '的位置: " + first);     //7
            Console.WriteLine("第二个'/ '的位置: " + second );    //12
            Console.WriteLine("第三个'/ '的位置: " + third );     //16

            int startIndex1 = 0;
            int length1 = first - 1;  //6--"所属机构名称"中"称"的位置
            Console.WriteLine("length1=" + length1);
            Console.WriteLine(s1.Substring(startIndex1,length1));   //所属机构名称--从第0个位置开始,6个字符
            Console.WriteLine("-------------------------------------------------");

            int startIndex2 = first ;    //7
            int length2 = (second-1) -first;       //4--"教师姓名"中"名"的位置-第一个"/"的位置
            Console.WriteLine("startIndex2=" + startIndex2);
            Console.WriteLine("length2=" + length2);
            Console.WriteLine(s1.Substring(startIndex2, length2));   //教师姓名--从第7个位置开始,4个字符
            Console.WriteLine("-------------------------------------------------");

            int startIndex3 = second  ;       //12
            int length3 = (third -1)-second ;       //4--"课程类型"中"型"的位置-第二个"/"的位置
            Console.WriteLine("startIndex3=" + startIndex3);
            Console.WriteLine("length3=" + length3);
            Console.WriteLine(s1.Substring(startIndex3, length3));   //课程类型--从第12个位置开始,4个字符
            Console.WriteLine("-------------------------------------------------");

            int startIndex4 = third ;   //17
            Console.WriteLine("startIndex4=" + startIndex4);
            Console.WriteLine(s1.Substring(startIndex4));   //课程名称--从第17个位置开始
            Console.WriteLine("-------------------------------------------------");

            
        }
    }






        Substring函数能实现字符串截取,一般和IndexOf函数一起使用。如果用Substring函数实现上面我们所需要的功能的话,逻辑有些复杂,代码太多,一不小心就容易出错。那么下一篇博客就教大家怎么用别的函数简单实现我们想要的字符串截取功能。




  • 8
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值