首页 新闻 会员 周边 捐助

C#字符串处理,通过波浪号合并

0
悬赏园豆:10 [已解决问题] 解决于 2018-07-27 14:37

产品有不同的型号,型号比如有a1,a2,b1,b2,b3,ab1,a 等等.

现在假设有a1,a2,a3,a4如何通过代码转换为a1~a4.

谢谢!

21天后的主页 21天后 | 初学一级 | 园豆:3
提问于:2014-05-25 16:11
< >
分享
最佳答案
0

var mystr = "产品有不同的型号,型号比如有ab1,ab2,b1,b2,b3,等等.现在假设有a1,a2,a3,a4";
var matches = Regex.Matches(mystr, @"([a-z]+)\d+\s*[,,]\s*(\1(\d+)(?:\s*[,,]\s*)*)*");
foreach (Match match in matches)
{
  if (string.IsNullOrEmpty(match.Groups[3].Value))
  {
    continue;
  }
  var str = match.Value;
  var chart = match.Groups[1].Value;

  var count = Convert.ToInt32(match.Groups[3].Value);
  if (count>1)
  {
    mystr = mystr.Replace(str, chart + "1~" + chart + count+",");
  }
}

Console.WriteLine(mystr);

收获园豆:10
shelher | 菜鸟二级 |园豆:216 | 2014-05-25 22:02

如果是: 'a1,a2,a3,a5,a7,a8,a9' 能处理成: 'a1~a3,a5,a7~a9'么?

袁家小黑球 | 园豆:1045 (小虾三级) | 2014-05-26 09:02

@袁家小黑球: 

private void Form1_Load(object sender, EventArgs e)
{
  var mystr = "产品有不同的型号,型号比如有ab1,ab2,b1,b2,b3,等等.现在假设有a1,a2,a3,a5,a7,a8,a9";
  var matches = Regex.Matches(mystr, @"([a-z]+)\d+\s*[,,]\s*(\1(\d+)(?:\s*[,,]\s*)*)*");
  foreach (Match match in matches)
  {
    if (string.IsNullOrEmpty(match.Groups[3].Value))
    {
      continue;
    }
    var str = match.Value;
    var chart = match.Groups[1].Value;

    var count = Convert.ToInt32(match.Groups[3].Value);
    if (count>1)
    {
      var intStr = str.Replace(chart, "").Replace(" ", "");
      var intLists = intStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
      if (intLists.Length == count)
      {
        mystr = mystr.Replace(str, chart + intLists[0] + "~" + chart + count + ",");
        continue;
      }

      var lists = new List<LianxuInt>();
      var start = Convert.ToInt32(intLists[0]);
      var mynum = Convert.ToInt32(intLists[0]);
      for (var i = 0; i < intLists.Length; i++)
      {
        var num = Convert.ToInt32(intLists[i]);
        if (num == mynum)
        {
          if (i == intLists.Length - 1)
          {
            var end = Convert.ToInt32(intLists[i]);
            var lianxu = new LianxuInt
            {
              StartInt = start,
              EndInt = end
            };
            lists.Add(lianxu);
            break;
          }
          mynum = Convert.ToInt32(intLists[i])+1;
        }
        else
        {
          var end = Convert.ToInt32(intLists[i-1]);
          var lianxu = new LianxuInt
          {
            StartInt = start,
            EndInt = end
          };
          lists.Add(lianxu);
          if (i==intLists.Length-1)
          {
            break;
          }
          start = mynum = Convert.ToInt32(intLists[i]);
          i--;
        }
      }

      var newStr = lists.Aggregate("", (current, list) => current + list.MyStr(chart));

      mystr = mystr.Replace(str, newStr);
      }
    }
    Console.WriteLine(mystr);
  }
}

/// <summary>
/// 连续整数的起始与结束
/// </summary>
public class LianxuInt
{
  public int StartInt { get; set; }
  public int EndInt { get; set; }

  public string MyStr(string chart)
  {
    return StartInt == EndInt ? chart + EndInt + "," : chart + StartInt + "~" + chart + EndInt + ",";
  }
}

 

输出为:产品有不同的型号,型号比如有ab1~ab2,b1~b3,等等.现在假设有a1~a3,a5,a7~a9,

 

 

shelher | 园豆:216 (菜鸟二级) | 2014-05-26 13:06
其他回答(1)
0

list<String>.Split()

平常心队长 | 园豆:1113 (小虾三级) | 2014-05-26 08:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册