C# 遍历字符串

有一个字符串‘1002,1003,1004,10015,100645’,我想把它遍历一次,然后建立一个数组,把逗号之间的字符存进数组里面去,用C#怎么做?

第1个回答  推荐于2017-09-30
string s = "abcd45612,asd";
int characters = 0;
int numbers = 0;
int symbols = 0;
foreach (char c in s)
{

if(char.IsPunctuation(c))
symbols++;
if(Char.IsLetter(c))
characters++;
if(char.IsDigit(c))
numbers++;
}
Console.WriteLine("共有{0}个字母,{1}个数字,{2}个标点", characters, numbers, symbols);
第2个回答  2008-10-24
String str = "1002, 1003, 1004, 1005, 100645";
String[] strs = str.Split(',');
Int32 arrInt = new Int32[strs.Length];
for (Int32 i = 0; i < strs.Length; i++)
{
arrInt[i] = Int32.Parse(strs[i].Trim());
}

现在arrInt就是你要的Int32数组.
第3个回答  2008-10-24
遍历只能用在数组、集合,字符串不能遍历的,要很把他划成数组后才可遍历,一楼的string s="1002,1003,1004,10015,100645";

string[] args = s.split(new char[]{','}); 是种方法
第4个回答  2008-10-24
string s="1002,1003,1004,10015,100645";

string[] args = s.split(new char[]{','});本回答被提问者采纳
第5个回答  2008-10-24
string.splite函数就可以

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网