将实际字符串值增加一的最简单方法
本文关键字:最简单 方法 增加 字符串 | 更新日期: 2023-09-27 18:21:44
我有一个字符串,需要与另一个字符串数组匹配。如果我发现两个数组值都匹配,那么我必须从字符串中分离出数值,并用该数值添加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
因此,当我找到与这些字符串匹配的字符串时,我需要得到像00023、00000101这样的数值,并将值增加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;
}
}
您可以使用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 });
为什么不简单地取字符串的右部分(数字部分),将其转换为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;
}
作为一个额外的提示,可以在增加int后简化字符串的创建,而不是计算字符串的长度并用零进行PadLeft,您还可以使用int并使用StringFormat IFormatProvider添加前导零:
string paddedString = String.Format("TS{1:00000}", count);
//when count = 24, should output "TS00024"