public static string UpperCaseStringSplitter(string stringToSplit)
{
var stringBuilder = new StringBuilder();
foreach (char c in stringToSplit)
{
if (Char.IsUpper(c) && stringToSplit.IndexOf(c) > 0)
stringBuilder.Append(" " + c);
else
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
If I pass a string like this:
TestSrak
the output is the expected one : "Test Srak".
But when there are two same letters where one is lower case and the other is Uppercase next to each other, the split does not happen:
For example If the input is:
TestTruck
The output is also TestTruck . Can You please tell me where is the problem and how can I fix it. Thanks!
The problem is this
In
"TestTruck"the first letter(index == 0) is also aT, therefore it will not enter theif.Instead i would use a
for-loopand check if the current char is the first, then you can skip the split: