【发布时间】:2020-02-04 20:35:27
【问题描述】:
我需要一段在“[]”括号中增加字符串结尾的代码,但我对此感到头疼。
问题是,如果给定集合中存在名称“test”,算法应该返回test_[0],如果两者都存在则“test_[1]”等。到目前为止有效。但是,当我尝试将 currentName 值作为“test_[something]”传递时,算法会创建类似test_[0]_[0]、test_[0]_[1] 而不是test_[somenthing+someNumber]。有谁知道改变这种行为的方法吗?
// test test, test_[2], test_[3]
protected string GetDistinctName2(string currentName, IEnumerable<string> existingNames)
{
int iteration = 0;
if (existingNames.Any(n => n.Equals(currentName)))
{
do
{
if (!currentName.EndsWith($"({iteration})"))
{
currentName = $"{currentName}_[{++iteration}]";
}
}
while (existingNames.Any(n => n.Equals(currentName)));
}
return currentName;
}
编辑: 到目前为止最好的解决方案是(我敢打赌我在这里看到过,但有人不得不删除)
public static void Main()
{
var currentOriginal = "test";
var existingNamesOriginal = new[] { "test", "test_[2]", "test_[3]" };
string outputOriginal = GetDistinctNameFromSO(currentOriginal, existingNamesOriginal);
Console.WriteLine("original : " + outputOriginal);
Console.ReadLine();
}
protected static string GetDistinctNameFromSO(string currentName,
IEnumerable<string> existingNames)
{
if (null == currentName)
throw new ArgumentNullException(nameof(currentName));
else if (null == existingNames)
throw new ArgumentNullException(nameof(existingNames));
string pattern = $@"^{Regex.Escape(currentName)}(?:_\[(?<Number>[0-9]+)\])?$";
Regex regex = new Regex(pattern);
var next = existingNames
.Select(item => regex.Match(item))
.Where(match => match.Success)
.Select(match => string.IsNullOrEmpty(match.Groups["Number"].Value)
? 1
: int.Parse(match.Groups["Number"].Value))
.DefaultIfEmpty()
.Max() + 1;
if (next == 1)
return currentName; // No existingNames - return currentName
else
return $"{currentName}_[{next}]";
}
对于给定的“test”字符串,它返回“test_[4]”,这很好,但如果给定的字符串是“test_[2]”,它也应该返回“test_[4]”(给定模式的字符串第一个空闲数字),但它返回“test_[2]_[2]”。
【问题讨论】:
-
感谢您抽出宝贵时间分享您的问题。你的问题质量很低。你的目标和困难是什么?到目前为止你做了什么?请尝试更好地解释您的问题、您的开发环境和数据结构,并分享更多代码(无屏幕截图)、屏幕图像或草图以及用户故事或场景图。为了帮助您改进查询,请考虑阅读 How do I ask a good question。
-
请详细一点,OP
-
不是传递
currentName = "test_[0]"作为参数,而是传递currentName = "test", iteration = 0,所以这两个部分(名称和迭代)在方法签名中都是可变的 -
基于格式化和比较字符串的迭代解决方案开始遇到几千个项目的性能问题,你同意吗?
-
在
EndsWith的测试中,您使用圆括号“()”,但输入包含方括号“[]”
标签: c# string algorithm linq ienumerable