Как можно улучшить нижеприведенный код, чтобы он работал максимально эффективно:
StringBuilder str = new StringBuilder("Lorem ipsum dolor sit amet, " +
"consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " +
"consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat " +
"nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt " +
"mollit anim id est laborum.");
int endIndex = 0;
for (int i = 0; i < str.Length-1; i++)
{
if (str[i] == ' ' && str[i+1] == ' ')
{
endIndex = i+1;
for (int j = endIndex; j < str.Length-1; j++)
{
if (str[j + 1] != ' ')
{
endIndex = j;
break;
}
}
str.Remove(i, endIndex-i);
i = endIndex - 1;
}
}
Console.WriteLine(str);