using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Splitstring
{
class Program
{
static void Main(string[] args)
{
string str = "123,2,3,434,5,6,7,8,9354";
string[] split = str.Split(new Char[] { ',' });
foreach (string s in split)
{
if (s.Trim() != ",")
Console.WriteLine(int.Parse(s));
}
}
}
}
下面是另一种方法,明显比我的要好,所以拿来膜拜一下~
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
string str="123,2,3,434,5,6,7,8,9354";
int[] arr = StringToIntArray(str);
foreach (int i in arr)
{
Console.WriteLine ;
}
}
static int[] StringToIntArray(string str)
{
string[] strarr = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int[] intarr = new int[strarr.Length];
for (int i = 0; i < intarr.Length; i++)
{
intarr[i] = Convert.ToInt32(strarr[i]);
}
return intarr;
}
}
}