c#中StartsWith和EndsWidth的用法

发布日期:2019-11-26 作者:mi52.com 人气:

c#中StartsWith和EndsWidth的用法:

public bool StartsWith( string value ) 

判断字符串实例的开头是否匹配指定的字符串。


C#开发过程中针对字符串String类型的操作是常见操作,有时候业务需要判断某个字符串是否以特定字符开头或者特定字符结束,此时就可使用StartsWith方法来判断目标字符串是否以特定字符串开头,通过EndWith方法可判断是否以某字符串结尾。


StartWith函数调用的格式为: strA.StartsWith(strB),判断strA字符串是否以strB字符串为开头。


EndWith函数调用的格式为:strA.EndWith(strB),判断strA字符串是否以strB字符串结尾。


举例如下,字符串str的值为YinSoo,判断str是否以Yin开头以及str是否以oo结尾可使用下列语句:


 string str = "TYinSoo";

 var start= str.StartsWith("Yin");//返回True,即以Tom开头

 var end = str.EndsWith("oo");//返回True,即以ey结尾


a.startsWith(b) --判断字符串a,是不是以字符串b开头

a.endsWith(b) --判断字符串a,是不是以字符串b结尾

  

判定一个字符串对象是否以另一个子字符串开头,如果是返回true;否则返回false。

string s = "aabbcc";

bool b1= s.StartsWith("aabb"); //返回的结果为true


----------------------------------


        string aa = "abc123456def";

        bool a1 = aa.StartsWith("a");

        Response.Write(a1);

        Response.Write("<br>");

        //结果是True


        bool a2 = aa.StartsWith("A");

        Response.Write(a2);

        Response.Write("<br>");

        //结果是False


        bool a3 = aa.StartsWith("ab");

        Response.Write(a3);

        Response.Write("<br>");

        //结果是True


        bool a4 = aa.StartsWith("abc12");

        Response.Write(a4);

        Response.Write("<br>");

        //结果是True


        bool a5 = aa.StartsWith("ab");

        Response.Write(a5);

        Response.Write("<br>");

        //结果是True



        Response.Write("================== <br>");

        string bb = "abc123456def";

        bool b1 = bb.EndsWith("f");

        Response.Write(a1);

        Response.Write("<br>");

        //结果是True


        bool b2 = bb.EndsWith("F");

        Response.Write(b2);

        Response.Write("<br>");

        //结果是False


        bool b3 = bb.EndsWith("def");

        Response.Write(b3);

        Response.Write("<br>");

        //结果是True


        bool b4 = bb.EndsWith("56def");

        Response.Write(b4);

        Response.Write("<br>");

        //结果是True


        Response.Write("================== <br>");

        string cc = "Abc12345";

        bool c1 = cc.StartsWith("a");

        Response.Write(c1);

        Response.Write("<br>");

        //结果是False


        bool c2 = cc.ToUpper().StartsWith("A");

        Response.Write(c2);

        Response.Write("<br>");

        //结果是True


        bool c3 = cc.ToUpper().StartsWith("AB");

        Response.Write(c3);

        Response.Write("<br>");

        //结果是True


        Response.Write("================== <br>");


        string[] str = new string[] { "www", "yinsoo", "com" };

        string ss = str.Where(c => c.StartsWith("yin")).FirstOrDefault();

        Response.Write(ss);

        //结果是yinsoo


返回顶部

拨打电话
首页