獲取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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章