I've built a string builder to add spaces into text if it is capital. The sentence entered would look like this : "ThisIsASentence." Since it starts with a capital, the string builder would modify the sentence to look like this: " This Is A Sentence."
My problem is, If I were to have a sentence like "thisIsASentence." the string builder will separate the sentence like normal : " this Is A Sentence."
Still both have a space in front of the first character.
When the sentence runs through this line:
result = result.Substring(1, 1).ToUpper() + result.Substring(2).ToLower();
If the first letter entered was lowercase, it gets cut off and the second letter becomes uppercase.
The line was meant to keep the first letter entered capitalized and set the rest lowercase.
Adding a trim statement before running that line changes nothing with the output.
Here is my overall code right now:
private void btnChange_Click(object sender, EventArgs e)
{
// New string named sentence, assigning the text input to sentence.
string sentence;
sentence = txtSentence.Text;
// String builder to let us modify string
StringBuilder sentenceSB = new StringBuilder();
/*
* For every character in the string "sentence" if the character is uppercase,
* add a space before the letter,
* if it isn't, add nothing.
*/
foreach (char c in sentence)
{
if (char.IsUpper(c))
{
sentenceSB.Append(" ");
}
sentenceSB.Append(c);
}
// Store the edited sentence into the "result" string
string result = sentenceSB.ToString();
// Starting at the 2nd spot and going 1, makes the first character capitalized
// Starting at position 3 and going to end change them to lower case.
result = result.Substring(1, 1).ToUpper() + result.Substring(2).ToLower();
// set the label text to equal "result" and set it visible.
lblChanged.Text = result.ToString();
lblChanged.Visible = true;