让我很郁闷的问题!!

CQ_alex 2010-10-09 11:42:49
string str ="1,2,3,4,5";
怎么变成
int 的1,2,3,4,5啊?
谢谢!!

C#的!
...全文
157 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
CQ_alex 2010-10-11
  • 打赏
  • 举报
回复
谢谢大伙啦!!!
chen_lis 2010-10-09
  • 打赏
  • 举报
回复
用Split函数 这个是他的用法

1、用字符串分隔:
using System.Text.RegularExpressions;
string str="aaajsbbbjsccc";
string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
foreach (string i in sArray) Response.Write(i.ToString() + "<br>");
输出结果:
aaa
bbb
ccc
2、用多个字符来分隔:
string str="aaajbbbscccjdddseee";
string[] sArray=str.Split(new char[2] {'j','s'});
foreach(string i in sArray) Response.Write(i.ToString() + "<br>");
输出结果:
aaa
bbb
ccc
ddd
eee
3、用单个字符来分隔:
string str="aaajbbbjccc";
string[] sArray=str.Split('j');
foreach(string i in sArray) Response.Write(i.ToString() + "<br>");
输出结果:
aaa
bbb
ccc


////////////////////////////////////////////////
string[] arr = str.Split("o");
这是一个具有语法错误的语句,Split 的 separator 参数应该是 char[] 或 string[],不应是字符串。正确的示例:

string str = "technology";
char[] separator = { 'o' };
string[] arr = str.Split(separator);
////////////////////////////////////////////////////

String.Split 方法有6个重载函数:
程序代码
1) public string[] Split(params char[] separator)
2) public string[] Split(char[] separator, int count)
3) public string[] Split(char[] separator, StringSplitOptions options)
4) public string[] Split(string[] separator, StringSplitOptions options)
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6) public string[] Split(string[] separator, int count, StringSplitOptions options)

下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
1. public string[] Split(params char[] separator)
程序代码
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
2. public string[] Split(char[] separator, int count)ITPUB个人空间,n:H!C0M/S3U\u0002P
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
3. public string[] Split(char[] separator, StringSplitOptions options)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
4. public string[] Split(string[] separator, StringSplitOptions options)
程序代码
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
\u0002w1I+Ch%^\u0017}0string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素ITPUB个人空间1K;e\u0007f\u0008f }\u0011C n
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
程序代码
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
需要注意的是没有重载函数public string[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')


先多看点书吧
ChargeForward 2010-10-09
  • 打赏
  • 举报
回复
这是最基本的东西了 这都郁闷?
tashiwoweiyi 2010-10-09
  • 打赏
  • 举报
回复

string str = "1,2,3,4,5";
string[] strList = str.Split(',');
List<int> nums = new List<int>();
for (int i = 0; i < strList.Length; i++)
{
nums.Add(int.Parse(strList[i].ToString()));
}

手抓宝 2010-10-09
  • 打赏
  • 举报
回复
using System.Linq;

string str = "1,2,3,4,5";
string[] s = str.Split(',');
int[] i = s.Select(j => Convert.ToInt32(j)).ToArray<int>();
勤奋的阿拉丁 2010-10-09
  • 打赏
  • 举报
回复
string str = "1,2,3,4,5";
string[] values = str.Split(',');
List<int> results = new List<int>();
foreach (string v in values)
{
try
{
results.Add(Convert.ToInt32(v));
}
catch (Exception)
{


}

}
huanshayi 2010-10-09
  • 打赏
  • 举报
回复
guozhaoyou1 2010-10-09
  • 打赏
  • 举报
回复
string [] ary=str.spilt(',')
foreach(string s in ary)
{
response.write(s)
int v=int.paser(s)
}
yuxh81 2010-10-09
  • 打赏
  • 举报
回复
试试:

int[] idList = str.split(",");
细嗅蔷薇 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hrabeyond 的回复:]

using System.Linq;

string str = "1,2,3,4,5";
string[] s = str.Split(',');
int[] i = s.Select(j => Convert.ToInt32(j)).ToArray<int>();
[/Quote]
+1
XiuJuan584868710 2010-10-09
  • 打赏
  • 举报
回复
split好啊!我刚学那会也郁闷,用的多了,就好了!
早安您好 2010-10-09
  • 打赏
  • 举报
回复
不错 学习了 。。
打一壶酱油 2010-10-09
  • 打赏
  • 举报
回复
int[] idList = Convert.toint32(str.split(","));
hongjiaoli 2010-10-09
  • 打赏
  • 举报
回复
string str = "1,2,3,4,5";
string[] strList = str.Split(',');
List<int> nums = new List<int>();
for (int i = 0; i < strList.Length; i++)
{
nums.Add(int.Parse(strList[i].ToString()));
}
porschev 2010-10-09
  • 打赏
  • 举报
回复
split切开成数组。。。

再转
mayanly 2010-10-09
  • 打赏
  • 举报
回复
问题都没太看明白,我也很郁闷!
村长_乐 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chen_lis 的回复:]
用Split函数 这个是他的用法

1、用字符串分隔:
using System.Text.RegularExpressions;
string str="aaajsbbbjsccc";
string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
foreach (string i in sArray) Respon……
[/Quote]
哥们,干嘛呢?这么详细?

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧