每次数字超过45时,如何将名称增加1?
简单地说,当我 NAME:45 时,我需要下一个名称为 NAME1:1 。 它非常简单。但问题是,我已经有一个计数器来看看从45到1的变化。这是因为我不知道如何处理上述情况? 我怎么能这样做?
我的代码段:
public void reccurent()
{ //Here we are exporting header
string[] strLines = System.IO.File.ReadAllLines(textBox1.Text);
string CarouselName = enter.Text;
int[] cols = new int[] { 15, 15, 25, 15, 15 };
StringBuilder sb = new StringBuilder();
string[] cells = line.Replace("\"", "").Split('\t');
//removing double woutes in the textfile
for (int c = 0; c < cells.Length; c++)
sb.Append(cells[c].Replace(" ", "_").PadRight(cols[c]));
//replacing all spaces with underscore in the header column
sb.AppendLine("Location".PadRight(15));
//adding a coumn called LOCATION
sb.AppendLine();
int repeater = 45; // change me
int tmpCarousel = 0;
int carouselNumber = 0;
Dictionary<string, int> namesForCarousels = new Dictionary<string, int>();
for (int i = 0; i < textfile.Count; i++)
{
for (int c = 0; c < cells.Length; c++)
sb.Append(textfile[i].Cells[c].Replace(" ", "_").PadRight(cols[c]));
//replacing all spaces with underscore in each column values
string name = textfile[i].Cells[1];
if (namesForCarousels.TryGetValue(name, out tmpCarousel) == false)
{
carouselNumber++;
if (carouselNumber > 45)
carouselNumber = 1;//resetting to number1, but name is
namesForCarousels[name] = carouselNumber;
}
string num = carouselNumber < repeater ? "" : (carouselNumber / repeater).ToString();
var strCorousel = lstMX.Find(x => x.MAX_PN.Equals(name)).Carousel;
strCorousel = (String.IsNullOrEmpty(strCorousel)) ? CarouselName : strCorousel;
sb.Append(String.Format("{0}:{1}", strCorousel + num, 1 + carouselNumber % repeater).PadRight(15));
// carouselNumber++;
sb.Append("\r\n");
}
System.IO.File.WriteAllText(@"D:\output.TXT", sb.ToString());
}
输出为:
NAME:2 // name is stating from2...
NAME:3
.
.
.
NAME:45 //after the 45.. its going perfect to NAME1:1
NAME1:1 //but after NAME1:1 its going to NAME
NAME:2
答案 0 :(得分:2)
这是一种方法。显然有很多方法可以做到这一点。
var testCounter = "NAME1";
for (int x = 0; x < 50; x++)
{
Match m = Regex.Match(testCounter, @"(?<label>NAME)(?<counter>\d+)(?::(?<robinCounter>\d+))?");
if (m.Success)
{
var count = Int32.Parse(m.Groups["counter"].Value);
var roundRobinCounter = 0;
if (m.Groups["robinCounter"].Success)
roundRobinCounter = Int32.Parse(m.Groups["robinCounter"].Value);
count++;
if (count == 46)
{
count = 1;
roundRobinCounter++;
}
var sb = new StringBuilder();
sb.AppendFormat("{0}{1}", m.Groups["label"].Value, count);
if (roundRobinCounter != 0)
sb.AppendFormat(":{0}", roundRobinCounter);
testCounter = sb.ToString();
Console.WriteLine(testCounter);
}
}
每次需要递增时,它只使用正则表达式来解析文本。我没有包含所有的错误处理,但你应该可以从这里拿起它。
这是正则表达式的细分
(?<label> -- Start a named group capture called "label"
NAME -- Search for the literal "NAME"
)
(?<counter> -- Start a named group capture called "counter"
\d+ -- Search for one or more digits
)
(?: -- Start a non-capture group
: -- Find a literal colon, and stop the previous digit search
(?<robinCounter> -- Start a named group capture called "robinCounter"
\d+ -- Find one or more digits (continues to end of the string or when a non-didgit is found)
)
)? -- End the non-capture group, and make the group optional (the question mark means find 0 or 1 time)
编辑我更新了代码以反映您提供的输出。
以上代码中的一些示例输出:
NAME40
NAME41
NAME42
NAME43
NAME44
NAME45
NAME1:1
NAME2:1
NAME3:1
NAME4:1
NAME5:1
NAME6:1
答案 1 :(得分:0)
示例3而不是45
int repeater = 3; // change me
string name = "NAME";
int carouselNumber = 0;
for (var i = 0; i < 50; i++)
{
string num = carouselNumber < repeater ? ":" : (carouselNumber / repeater).ToString() + ":";
Console.WriteLine("{0}\t{1}", name + num, 1 + carouselNumber % repeater);
carouselNumber++;
}
有关
NAME: 1
NAME: 2
NAME: 3
NAME1: 1
NAME1: 2
NAME1: 3
NAME2: 1
NAME2: 2
NAME2: 3
NAME3: 1
NAME3: 2
NAME3: 3
NAME4: 1
NAME4: 2
NAME4: 3
NAME5: 1
NAME5: 2
NAME5: 3
NAME6: 1
NAME6: 2
NAME6: 3