String取值的问题(很实用的方法)

本文介绍了一种使用 C# 对复杂字符串进行处理的方法,包括如何从特定格式的字符串中提取数字并转换为整数数组,同时展示了如何进一步解析另一种格式的字符串数组。

string hits=@"1,58; 1167; 1,11; 2154; 3,15; 4,11; 4,16; 14;13 ";请问如何把hits如1,58 1167 的头个数字取出来,得到一个数组里面是1 2 3 4 14.

string   hits=@"1,58;   1,167;   1,11;   2,154;   3,15;   4,11;   4,16;   14,13   ";   
    
  
string   []   temp   =   hits.Split(';');   
  
int   []   result   =   new   int[temp.Length];   
  
int   count   =   1;   
    
  result[
0]   =   Convert.ToInt32   (   temp[0].Split(',')[0]   );   
    
  
for   (   int   i   =   1;   i   <   temp.Length;   i++   )   
  
{   
  
int   num   =   Convert.ToInt32   (   temp[i].Split(',')[0]   );   
  
if   (   result[count   -   1]   !=   num   )   
  
{   
  result[count]   
=   num;   
  count
++;   
  }
   
  }

 

String answer = "1:3;2:2";
//将String用;分成一个字符串数组
string [] result= answer.Split(';');
//可以再去每个的制
for (int i = 0; i < result.Length-1; i++)
{
  
int b=Convert.ToInt32(result[i-1].Split(':')[1]);
  MessageBox.Show(
"数字{0}",b);
}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值