1

我有一个字符串,我需要与另一个字符串数组匹配。如果我发现两个数组值都匹配,那么我必须从字符串中拆分出数值,然后用该数值加1,然后用新值附加实际字符串。我尝试了一些拆分和连接。但是我没有找到解决问题的正确有效的方法。下面是我的场景。

实际字符串:

BK-TS00023,X1-TS00000101,X4-A10000024,Y1-3,

比较字符串

BK Books      // Compare first two characters => BK Books matches with BK-TS00023
X1 Serials    // Compare first two characters => X1 Serials matches with X1-TS00000101

因此,当我发现与这些字符串匹配时,我需要获取0002300000101之类的数值并将值增加 1。然后附加默认字符串。结果字符串将是这样的。

结果字符串

'BK-TS00024',
'X1-TS00000102',
'X4-A10000025',
'Y1-4'

我尝试了以下方式。请任何人帮助我提供执行此方案的正确方法。

代码

InfoType="BK Books"; // or "X1 Serials" // or etc ..
var splitInfo = InfoType.Split(' ');
        var SiteFileInfo = Db.SiteFiles.Where(asd => asd.Code == "AutoBarcode").Select(asd => asd.Line1).FirstOrDefault();
        var splitSiteFile = SiteFileInfo.Split(',');
        int cnt = 0;
        foreach (var s in splitSiteFile)
        {
            cnt += 1;
            if (s.Contains(splitInfo[0]))
            {

                //var infoSiteSplit = s.Split('-');
                var olyNumber = Regex.Split(s, @"(?<=\p{L})(?=\p{N})");
                int i = 0;
                string Truncstring = "";
                foreach (var a in olyNumber)
                {
                    bool result = int.TryParse(a, out i);
                    if (result)
                    {
                        i += 1;
                        int befconv = Convert.ToInt32(a);
                        Truncstring = s.Replace(befconv.ToString(), i.ToString());
                    }
                }
                splitSiteFile[cnt - 1] = Truncstring;
                string JoinString = string.Join(",", splitSiteFile);
                Db.ExecuteStoreCommand("update SiteFile set Line1={0} where Code={1}", JoinString, "AutoBarcode");
                Db.SaveChanges();
                return Truncstring;
            }
        }
4

3 回答 3

1

为什么不简单地取字符串的正确部分(数字部分),将其转换为 int,增加它并再次转换为字符串?

就像是:

public string IncreaseByOne(string original) {
  var numberpart = "";
  var index = original.Length - 1;
  while (index != 0) {
    var oneletter = original.Substring(index, 1);
    var isint = int.TryParse(oneletter, out digit);
    if (!isint) break;
    numberpart += oneletter;
    --index;
  }
  var firstpart = original.Substring(0, orignal.Length - numberpart.Length);
  var padlength = numberpart.Length;
  int value;
  int.TryParse(numberpart, out value);
  var result = ++value.ToString().PadLeft(padlength, '0');
  return firstpart + result;
}
于 2013-07-10T12:28:58.500 回答
1

就像在增加 int 后可以简化字符串创建的额外提示一样,您也可以采用 int 并使用 StringFormat IFormatProvider 添加前导零,而不是计算字符串的长度并使用零执行 PadLeft:

string paddedString = String.Format("TS{1:00000}", count); 
//when count = 24, should output "TS00024"
于 2013-07-10T12:38:16.403 回答
1

您可以使用Regex.Replace Method (String, String, MatchEvaluator)(并且可以选择使用委托而不是MatchEvaluator实例)。

试试这个(过滤器中的所有匹配项都会增加 1):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        var input = "BK-TS00023,X1-TS00000101,X4-A10000024,Y1-3,";
        var filter = new[] { "BK Books", "X1 Serials" };
        Console.WriteLine(input);
        var result = IncreaseWithFilter(input, filter);
        Console.WriteLine(result);
    }

    private static string IncreaseWithFilter(
        string input,
        IEnumerable<string> filter)
    {
        var truncatedFilter = filter.Select(f => f.Substring(0, 2));
        var result = Regex.Replace(input, @"([^,].*?)\d+(?=,)",
            (match1) =>
            {
                var value = match1.Value;
                if (truncatedFilter.Any(f => match1.Value.StartsWith(f)))
                {
                    value = Regex.Replace(match1.Value, @"(?<=)\d+",
                        (match2) =>
                        {
                            return (Convert.ToInt32(match2.Value) + 1)
                                .ToString()
                                .PadLeft(match2.Value.Length, '0');
                        });
                }
                return value;
            });
        return result;
    }
}

输出:

BK-TS00023,X1-TS00000101,X4-A10000024,Y1-3,
BK-TS00024,X1-TS00000102,X4-A10000024,Y1-3,

代码中的示例用法:

InfoType = "BK Books";
var SiteFileInfo = Db.SiteFiles
    .Where(asd => asd.Code == "AutoBarcode")
    .Select(asd => asd.Line1)
    .FirstOrDefault();
var result = IncreaseWithFilter(
    SiteFileInfo, 
    new[] { InfoType });
于 2013-07-10T11:47:58.553 回答