首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Csharp: Searching Within a String

Csharp: Searching Within a String

作者头像
geovindu
发布2026-06-18 21:53:07
发布2026-06-18 21:53:07
270
举报
代码语言:javascript
复制
 /// <summary>
    /// 20130118
    /// 搜索字符串
    /// 塗聚文 締友計算機信息技術有限公司
    /// 捷為工作室
    /// </summary>
    public class StringSearchClass
    {
        /// <summary>
        /// 查找字符串
        /// StringIndexOf("1,2,3,4,5,6", "1,8");
        /// </summary>
        /// <param name="str1">字符串</param>
        /// <param name="judgestr">找查的字符串</param>
        /// <returns>返回是否存在</returns>
        public static bool StringIndexOf(string str1, string searchstr)
        {
            bool isExist = true;
            str1 = "," + str1 + ",";
            string[] strs = searchstr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string strtemp in strs)
            {
                if (str1.IndexOf(',' + strtemp + ',') == -1)
                {
                    isExist = false;
                    break;
                }
            }
            return isExist;
        }
        /// <summary>
        /// 查找字符串 
        /// 塗聚文
        /// </summary>
        /// <param name="str1">字符串</param>
        /// <param name="judgestr">找查的字符串</param>
        /// <returns></returns>
        public static bool StringContains(string str1, string searchstr)
        {
            bool isExist = true;
            str1 = "," + str1 + ",";
            string[] strs = searchstr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string strtemp in strs)
            {
                if (!str1.Contains(',' + strtemp + ','))
                {
                    isExist = false;
                    break;
                }
            }
            return isExist;
        }
        /// <summary>
        /// 查找字符串
        /// 塗聚文
        /// </summary>
        /// <param name="str1">字符串</param>
        /// <param name="judgestr">找查的字符串</param>
        /// <returns></returns>
        public static bool StringRegex(string str1, string searchstr)
        {
            bool isExist = true;
            str1 = "," + str1 + ",";
            string[] strs = searchstr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string strtemp in strs)
            {
                if (!Regex.Match(str1, strtemp, RegexOptions.IgnoreCase).Success)
                {
                    isExist = false;
                }
            }
            return isExist;
        }
    }

測出試:

代码语言:javascript
复制
//測試
            Stopwatch sw = new Stopwatch();
            
            sw.Start();
           // bool s = StringIndexOf("1,2,3,4,5,6", "1,6");//True,所耗時間:00:00:00.0003557
           // bool s = StringContains("1,2,3,4,5,6", "1,6");//True,所耗時間:00:00:00.0003625
            bool s = StringRegex("1,2,3,4,5,6", "1,6");//False,所耗時間:00:00:00.0286617 //True,所耗時間:00:00:00.0008295 //True,所耗時間:00:00:00.0008237
           sw.Stop();
           this.textBox1.Text=s.ToString()+",所耗時間:"+sw.Elapsed.ToString();
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档