从字符串数组中获取某几个字符

本文通过示例对比了C#中字符串数组使用contains与indexOf方法的区别。contains适用于完整匹配搜索,而indexOf则提供更灵活的字符串定位。

在做字符检索的时候发现可以用contains和indexof,所以写了段小代码试了一下区别:

    static void Main(string[] args)
        {
            string[] fruitsArr = { "Apple 1.1","Apple 2.1","Pear","Orange"};
            bool arrEn1 = fruitsArr.Contains("Apple 1");
            bool arrEn2 = fruitsArr.Contains("Apple 1.1");
           Console.WriteLine("The arr result is {0},{1}",arrEn1,arrEn2);

            foreach (string ss in fruitsArr)
            {
                int aa = ss.IndexOf("Apple 1");
                if (ss.Contains("Apple 1"))
                {
                    Console.WriteLine("The string {0} is ,{1}",aa,ss);
                }
            }

            Console.ReadLine();

        }
结果如图:

结论:

1. contains可以用于在字符串和字符串数组中,但是在在数组中时,只能检索出字符串完全一致的字符串,只有一段时,是无法检索出来的。

2.indexof只能用于字符串检索,且返回具体索引,从0开始 。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值