I want to split a string without using the String.Split method.
I found a possible solution here. The code that I use is from the second answer.
This is my code:
public string[] SplitString(string input, char delimiter)
{
List<String> parts = new List<String>();
StringBuilder buff = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
if (input[i] == delimiter)
{
parts.Add(buff.ToString());
buff.Clear();
}
else
{
buff.Append(input[i]);
}
}
return parts.ToArray();
}
My problem here is that when I try to split a string like this
dog cat car person by " ", the result contains the words without the last one (in this example - person).
If after the last word there is a white space, the result is correct.
I tried to add something like i == input.Length when the for loop is from 0 to i <= input.Length. But the result was still without the last word.
Am I missing something?