Fastest way to remove first char in a String

The second option really isn’t the same as the others – if the string is “///foo” it will become “foo” instead of “//foo”. The first option needs a bit more work to understand than the third – I would view the Substring option as the most common and readable. (Obviously each of them as an individual statement … Read more

How to trim whitespace from a Bash variable?

I have a shell script with this code: But the conditional code always executes, because hg st always prints at least one newline character. Is there a simple way to strip whitespace from $var (like trim() in PHP)? or Is there a standard way of dealing with this issue? I could use sed or AWK, but I’d like to think there is a more elegant … Read more

How to trim a std::string?

EDIT Since c++17, some parts of the standard library were removed. Fortunately, starting with c++11, we have lambdas which are a superior solution. Thanks to https://stackoverflow.com/a/44973498/524503 for bringing up the modern solution. Original answer: I tend to use one of these 3 for my trimming needs: They are fairly self-explanatory and work very well. EDIT: BTW, I have std::ptr_fun in … Read more

How to trim whitespace from a Bash variable?

A simple answer is: Xargs will do the trimming for you. It’s one command/program, no parameters, returns the trimmed string, easy as that! Note: this doesn’t remove all internal spaces so “foo bar” stays the same; it does NOT become “foobar”. However, multiple spaces will be condensed to single spaces, so “foo bar” will become “foo bar”. In addition it doesn’t remove … Read more

Remove all whitespace in a string

If you want to remove leading and ending spaces, use str.strip(): If you want to remove all space characters, use str.replace(): (NB this only removes the “normal” ASCII space character ‘ ‘ U+0020 but not any other whitespace) If you want to remove duplicated spaces, use str.split():

Remove all whitespace in a string

If you want to remove leading and ending spaces, use str.strip(): If you want to remove all space characters, use str.replace(): (NB this only removes the “normal” ASCII space character ‘ ‘ U+0020 but not any other whitespace) If you want to remove duplicated spaces, use str.split():