How to remove all white space from the beginning or end of a string?

String.Trim() returns a string which equals the input string with all white-spaces trimmed from start and end: ” A String “.Trim() -> “A String” String.TrimStart() returns a string with white-spaces trimmed from the start: ” A String “.TrimStart() -> “A String ” String.TrimEnd() returns a string with white-spaces trimmed from the end: ” A String … Read more

Optional Whitespace Regex

Add a \s? if a space can be allowed. \s stands for white space ? says the preceding character may occur once or not occur. If more than one spaces are allowed and is optional, use \s*. * says preceding character can occur zero or more times. ‘#<a href\s?=”https://stackoverflow.com/questions/14293024/(.*?)” title\s?=”https://stackoverflow.com/questions/14293024/(.*?)”><img alt\s?=”https://stackoverflow.com/questions/14293024/(.*?)” src\s?=”https://stackoverflow.com/questions/14293024/(.*?)”[\s*]width\s?=”150″[\s*]height\s?=”https://stackoverflow.com/questions/14293024/(.*?)”></a>#’ allows an optional … Read more

Remove all whitespace in a string

If you want to remove leading and ending spaces, use str.strip(): >>> ” hello apple “.strip() ‘hello apple’ 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): >>> ” hello apple “.replace(” “, “”) ‘helloapple’ If you … Read more