首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2013-10-16 
获取string里以某一标记符隔离的所有子串先查询查询string里有多少个相同的子串,然后剪断字符串得到所有子

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


热点排行