先查詢查詢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();
}
}
}