You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^ [ \t ] + and replace with nothing to delete leading whitespace (spaces and tabs). Search for [ \t ] + $ to trim trailing whitespace. Do both by combining the regular expressions into ^ [ \t ] + | [ \t ] + $ . Instead of [ \t ] which matches a space or a tab, you can expand the character class into [ \t \r \n ] if you also want to strip line breaks. Or you can use the shorthand \s instead.