字符串取值问题

lhf3377 2010-10-08 08:45:37
string: 10003-45000 这样的字符串

取出10003 赋值到int
取出45000 赋值到int

这两个数不一定是五位数。
不用数组方式如何最简短代码取出
...全文
136 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2010-10-08
  • 打赏
  • 举报
回复
今天CSDN真是抽风的可以
wuyq11 2010-10-08
  • 打赏
  • 举报
回复
string str = "10003-45000";
List<int> lst=new List<int>();
foreach(string s in str.Split(new string[]{"-"}, StringSplitOptions.RemoveEmptyEntries);
{
list.Add(Convert.ToInt32(s));
}
sweetqueen1 2010-10-08
  • 打赏
  • 举报
回复
不过lz看样子是刚注册的、、结贴还是个问题、、会不会给分我们呀?
sweetqueen1 2010-10-08
  • 打赏
  • 举报
回复
如果确定只有两个数、用substring就可以了、、
string str="10003-45000"
string str1 =str.substring(0,str.indexof("-"));//结果是10003
string str2=str.substring(str.indexof("-")+1);//结果是45000
2342-454-4655-343
不确定就用split,string[] array=str.split(new char[]{'-'});
截取出来的就是
2342
454
4655
343

最后,这个都是字符串,转换成int就可以了、、int a=Convert.ToInt32(str)
sweetqueen1 2010-10-08
  • 打赏
  • 举报
回复
如果确定只有两个数、用substring就可以了、、
string str="10003-45000"
string str1 =str.substring(0,str.indexof("-"));//结果是10003
string str2=str.substring(str.indexof("-")+1);//结果是45000
2342-454-4655-343
不确定就用split,string[] array=str.split(new char[]{'-'});
截取出来的就是
2342
454
4655
343

最后,这个都是字符串,转换成int就可以了、、int a=Convert.ToInt32(str)
sweetqueen1 2010-10-08
  • 打赏
  • 举报
回复
如果确定只有两个数、用substring就可以了、、
string str="10003-45000"
string str1 =str.substring(0,str.indexof("-"));//结果是10003
string str2=str.substring(str.indexof("-")+1);//结果是45000
2342-454-4655-343
不确定就用split,string[] array=str.split(new char[]{'-'});
截取出来的就是
2342
454
4655
343

最后,这个都是字符串,转换成int就可以了、、int a=Convert.ToInt32(str)
sweetqueen1 2010-10-08
  • 打赏
  • 举报
回复
如果确定只有两个数、用substring就可以了、、
string str="10003-45000"
string str1 =str.substring(0,str.indexof("-"));//结果是10003
string str2=str.substring(str.indexof("-")+1);//结果是45000
2342-454-4655-343
不确定就用split,string[] array=str.split(new char[]{'-'});
截取出来的就是
2342
454
4655
343

最后,这个都是字符串,转换成int就可以了、、int a=Convert.ToInt32(str)
sweetqueen1 2010-10-08
  • 打赏
  • 举报
回复
如果确定只有两个数、用substring就可以了、、
string str="10003-45000"
string str1 =str.substring(0,str.indexof("-"));//结果是10003
string str2=str.substring(str.indexof("-")+1);//结果是45000
2342-454-4655-343
不确定就用split,string[] array=str.split(new char[]{'-'});
截取出来的就是
2342
454
4655
343

最后,这个都是字符串,转换成int就可以了、、int a=Convert.ToInt32(str)
风骑士之怒 2010-10-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyazhe 的回复:]
C# code
string s = "10003-45000";
string[] fields = s.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
int a, b;
if (!int.TryParse(fields[0], out a) || !int.TryParse(fields[1], out……
[/Quote]
split返回的还是数组啊。。
风骑士之怒 2010-10-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyazhe 的回复:]
C# code
string s = "10003-45000";
string[] fields = s.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
int a, b;
if (!int.TryParse(fields[0], out a) || !int.TryParse(fields[1], out……
[/Quote]
split返回的还是数组啊。。
兔子-顾问 2010-10-08
  • 打赏
  • 举报
回复
string s = "10003-45000";
string[] fields = s.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
int a, b;
if (!int.TryParse(fields[0], out a) || !int.TryParse(fields[1], out b)) return;
Console.WriteLine(a);
Console.WriteLine(b);
q107770540 2010-10-08
  • 打赏
  • 举报
回复

void Main()
{
string ss = "10003-45000 ";
foreach (Match m in Regex.Matches(ss, @"\d*"))
{
Console.WriteLine(m.Value);
}
}
//10003
//45000
q107770540 2010-10-08
  • 打赏
  • 举报
回复

void Main()
{
string ss = "10003-45000 ";
foreach (Match m in Regex.Matches(ss, @"\d*"))
{
Console.WriteLine(m.Value);
}
}
//10003
//45000
q107770540 2010-10-08
  • 打赏
  • 举报
回复

void Main()
{
string ss = "10003-45000 ";
int m =int.Parse( ss.Substring(0, ss.IndexOf("-")));
int n = int.Parse(ss.Replace(m + "-",""));
Console.WriteLine( m+" "+n);
}
//10003 45000

chuxue1342 2010-10-08
  • 打赏
  • 举报
回复
string str="10003-45000";
string[] strs=str.split('-');不行?
q107770540 2010-10-08
  • 打赏
  • 举报
回复

void Main()
{
string ss = "10003-45000 ";
int m =int.Parse( ss.Substring(0, ss.IndexOf("-")));
int n = int.Parse(ss.Replace(m + "-",""));
Console.WriteLine( m+" "+n);
}
//10003 45000

chuxue1342 2010-10-08
  • 打赏
  • 举报
回复
string str="10003-45000";
string[] strs=str.split('-');不行?
q107770540 2010-10-08
  • 打赏
  • 举报
回复

void Main()
{
string ss = "10003-45000 ";
int m =int.Parse( ss.Substring(0, ss.IndexOf("-")));
int n = int.Parse(ss.Replace(m + "-",""));
Console.WriteLine( m+" "+n);
}
//10003 45000

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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