I would like to discard the remaining characters (which can be any characters) in my string after I encounter a space.
Eg. I would like the string “10 1/2” to become “10”;
Currently I’m using Split, but this seems like overkill:
string TrimMe = "10 1/2";
string[] cleaned = TrimMe.Split(new char[] {' '});
return cleaned[0];
I feel there should be an easier way.
Some other options:
However, IMO what you started with is much simpler and easier to read.
EDIT: Both solutions will handle empty strings, return the original if no spaces were found, and return an empty string if it starts with a space.