Here's a way. Not fully Linq but using a little bit in the for loop. Also passes a few more unit tests than your original.
[EDIT]: After re-reading your question and some comments by other reviewers I added a few more unit tests and low and behold my original answer broke. So I've re-worked it and now made it even more complicated :) but passes the new unit tests I put in 10,11 and 12.
Whether your code needs to pass thoses tests or not though I'll leave that for you to decide.
As for optimization. I haven't profiled it so can't comment there.
[TestMethod]
public void Test()
{
var result = CreateText("t", new string[] { "t", "t(1)", "t(2)", "t(3)", "t(4)", "t(5)", "t(6)", "t(7)", "t(8)", "t(9)", "t(10)" });
Assert.IsTrue(result == "t(11)");
}
[TestMethod]
public void Test1()
{
var result = CreateText("t", new string[] { "t" });
Assert.AreEqual("t(1)", result);
}
[TestMethod]
public void Test2()
{
var result = CreateText("t", new string[] { "t", "t(4)", "t(2)", "z(3)", "t (45)", "t5)", "t(10)", "t(12)", "t( 89 ),ttt, t, t(23), t(8, t(we", "t( 89 )", "t(23)", "t(8", "t(we" });
Assert.AreEqual("t(90)", result);
}
[TestMethod]
public void Test3()
{
var result = CreateText("t", new string[] { });
Assert.IsTrue(result == "t");
}
[TestMethod]
public void Test4()
{
var result = CreateText("z", new string[] { "t(1)" });
Assert.AreEqual("z", result);
}
[TestMethod]
public void Test5()
{
var result = CreateText("z", new string[] { "t(-1)" });
Assert.AreEqual("z", result);
}
[TestMethod]
public void Test6()
{
var result = CreateText("t", new string[] { " t(0)" });
Assert.AreEqual("t(1)", result);
}
[TestMethod]
public void Test7()
{
var result = CreateText("t", new string[] { " t(9) " });
Assert.AreEqual("t(10)", result);
}
[TestMethod]
public void Test8()
{
var result = CreateText("t", new string[] { "t(1)", "t(2)", "t(3)", "t(4)", "t(5)", "t(6)", "t(7)", "t(8)", "t(9)", "test(10)" });
Assert.AreEqual("t(10)", result);
}
[TestMethod]
public void Test9()
{
var result = CreateText("t", new string[] { "t(1)", "t(2)", "t(3)", "t(4)", "t(5)", "t(6)", "t(7)", "t(8)", "t (9)", "test(10)" });
Assert.AreEqual("t(10)", result);
}
[TestMethod]
public void Test10()
{
var result = CreateText("t", new string[] { "t(1)", "t(2)", "t(3)", "t(4)", "t(5)", "t(6)", "t(7)", "t t(8)", "t (9)", "test(10)" });
Assert.AreEqual("t(10)", result);
}
[TestMethod]
public void Test11()
{
var result = CreateText("t", new string[] { "t(1)", "t(2)", "t(3)", "t(4)", "t((((24)", "ttt(22)", "t(7)", "t t(8)", "t (9)", "t(1)" });
Assert.AreEqual("t(10)", result);
}
[TestMethod]
public void Test12()
{
var result = CreateText("t", new string[] { "t(1)", "t(2)", "t(3)", "t(4)", "t((((24)", "ttt(22)", "t(29)) ))", "t t(8)", "t (9)", "t(1)" });
Assert.AreEqual("t(30)", result);
}
private static String CreateText(string textIdentifier, IEnumerable<string> texts)
{
const char startDeliminator = '(';
const char endDeliminator = ')';
int nextCount = 0;
foreach (String text in texts.Select(p => p.Trim()).Where(p => p.StartsWith(textIdentifier)))
{
short addCount = 0;
var startDelminatorIndex = text.LastIndexOf(startDeliminator);
// there must be at least one start deliminator
if (startDelminatorIndex >= 0)
{
var endDeliminatorIndex = text.IndexOf(endDeliminator, startDelminatorIndex);
var textName = text.Substring(0, startDelminatorIndex).Trim();
if (textName.Equals(textIdentifier, StringComparison.CurrentCultureIgnoreCase))
{
if (endDeliminatorIndex > startDelminatorIndex)
{
startDelminatorIndex++; // get past the (
var size = endDeliminatorIndex - startDelminatorIndex;
Int16.TryParse(text.Substring(startDelminatorIndex, size).Trim(), out addCount);
}
}
}
nextCount = Math.Max(nextCount, addCount + 1);
}
return nextCount == 0 ? textIdentifier : string.Format("{0}({1})", textIdentifier, nextCount);
}