Please check if this piece of code if good or could be better if yes then how and why?
I think its purpose is pretty obvious so I will skip this part. The only thing I can think of is using while instead of for. Anything else can be done better or more efficient way?
- public static string GetUniqueName(List<string> existedNames, string name)
- {
- name = Regex.Replace(name, @" \([0-9]+\)$", "");
-
- if (existedNames.Any(n => n == name) == false)
- return name;
-
- for (int j = 0; j < 1000; j++)
- {
- var newName = name + " (" + j + ")";
- if (existedNames.Any(n => n == newName) == false)
- {
- return newName;
- }
- }
-
- return "";
- }