读书人

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

发布时间: 2013-10-16 11:29:46 作者: rapoo

获取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();        }    }}


读书人网 >编程

热点推荐