查找匹配的订单号

刚刚有网友在QQ问及,根据订单前缀,去查找与前缀匹配的订单号。

Insus.NET在控制台应用程序中,使用普通的方法来实现,参考下面代码示例:

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplicationDemo.Geometric;

namespace ConsoleApplicationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>() {
                "XS443694739104075776","","YP443694739104075776"
            };

            var tList = new List<string>();

            foreach (var item in FilterPrefixOrderNoList)
            {
                foreach (var co in list)
                {
                    if (!string.IsNullOrEmpty(co) && co.Length >= item.Length && item == co.Substring(0, item.Length))
                    {
                        tList.Add(co);
                    }
                }
            }


            //输出
            foreach (var rst in tList)
            {               
                Console.WriteLine(rst);
            }
        }

        public static string[] FilterPrefixOrderNoList = { "TS", "XS", "YP" };
    }

}
Source Code

 

上面#6行代码,可以修改一下,更加简洁:

 

foreach (var item in FilterPrefixOrderNoList)
            {
                foreach (var co in list)
                {
                    // if (!string.IsNullOrEmpty(co) && co.Length >= item.Length && item == co.Substring(0, item.Length))

                    if (co.StartsWith(item))
                    {
                        tList.Add(co);
                    }
                }
            }
Source Code

 

posted @ 2019-05-14 11:25  Insus.NET  阅读(766)  评论(3编辑  收藏  举报