I want a clean looking function that will remove all whitespace that comes before and after a "block" of text/characters. So, if I have a string like:
"The barn ": Needs to become"The barn"." The barn": Needs to become"The barn"." The barn is small ":Needs to become
"The barn is small"
Etc. A "block of text" is essentially everything between the first non-whitespace character and the last non-whitespace character.
I was thinking of looping through a string:
- Until I encounter a non-whitespace character, I throw everything away.
- Once I encounter a non-whitespace character, I start saving it to a string I'll be returning.
- If I encounter a whitespace again, I save it to a temporary variable.
- If I encounter a non-whitespace character, I append the temp variable to the string i'll be returning, then repeat from step 3.
- If I don't encounter a non-whitespace character, I can discard the temp string and just return the string I've been saving.
I feel like this might be a bit messy though and could be done more elegantly? Maybe with some regex? Although I would agree that regex being less messy is really just a matter of opinion.