获取string里以某一标记符隔离的所有子串

先查询查询string里有多少个相同的子串,然后剪断字符串得到所有子串。

下例中隔离符号为","为例。

C#控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "sss,dddd,wedasdaa,tea,hhhhha,";
            string b = a;
            int count = 0;
            while (a.IndexOf(",") >= 0)//得到有多少个‘,’符号
            {
                count++;
                a = a.Substring(a.IndexOf(",") + 1, a.Length - a.IndexOf(",") - 1);
                Console.WriteLine(count + " " + a.IndexOf(",") + " " + a);
            }
            string[] stringList = new string[count];
            for(int i=0;i<count;i++)//剪断字符串,赋值给stringList
            {
                stringList[i] = b.Substring(0, b.IndexOf(","));
                b = b.Substring(b.IndexOf(",") + 1, b.Length - b.IndexOf(",") - 1);
                Console.WriteLine(stringList[i]);
            }
            Console.ReadKey();
        }
    }
}


发布了90 篇原创文章 · 获赞 50 · 访问量 14万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章