Trim unicode whitespace in PHP
preg_replace(‘/^[\pZ\pC]+|[\pZ\pC]+$/u’,”,$str);
preg_replace(‘/^[\pZ\pC]+|[\pZ\pC]+$/u’,”,$str);
Strip all whitespace from the left end of the title: <?php echo ltrim(wp_title(”)); ?> Strip all whitespace from either end: <?php echo trim(wp_title(”)); ?> Strip all spaces from the left end of the title: <?php echo ltrim(wp_title(”), ‘ ‘); ?> Remove the first space, even if it’s not the first character: <?php echo str_replace(‘ ‘, … Read more
This looks for at least one non whitespace character. /\S/.test(” “); // false /\S/.test(” “); // false /\S/.test(“”); // false /\S/.test(“foo”); // true /\S/.test(“foo bar”); // true /\S/.test(“foo “); // true /\S/.test(” foo”); // true /\S/.test(” foo “); // true I guess I’m assuming that an empty string should be consider whitespace only. If an … Read more
You could use String.Replace method string str = “C Sharp”; str = str.Replace(” “, “”); or if you want to remove all whitespace characters (space, tabs, line breaks…) string str = “C Sharp”; str = Regex.Replace(str, @”\s”, “”);
Use preg_match as suggested by Josh: <?php $foo = ‘Bob Williams’; $bar=”SamSpade”; $baz = “Bob\t\t\tWilliams”; var_dump(preg_match(‘/\s/’,$foo)); var_dump(preg_match(‘/\s/’,$bar)); var_dump(preg_match(‘/\s/’,$baz)); Ouputs: int(1) int(0) int(1)
The fastest way? Iterate over the string and build a second copy in a StringBuilder character by character, only copying one space for each group of spaces. The easier to type Replace variants will create a bucket load of extra strings (or waste time building the regex DFA). Edit with comparison results: Using http://ideone.com/NV6EzU, with … Read more
there are many ways, but here’s a simple, easy to understand way. add these lines to your ~/.vimrc: autocmd FileType html setlocal ts=2 sts=2 sw=2 autocmd FileType ruby setlocal ts=2 sts=2 sw=2 autocmd FileType javascript setlocal ts=4 sts=4 sw=4
I found the answer here. Adding the following to my .vimrc file did the trick: autocmd BufWritePre *.py :%s/\s\+$//e The e flag at the end means that the command doesn’t issue an error message if the search pattern fails. See :h :s_flags for more.
the thing is, that method in ruby can be run with or without parentheses. for example, you can run Array.new 1,2 and ruby knows that it receives the arguments after the space. and you can also run Array.new(1,2) and ruby knows the args are inside the parentheses. but, when you run Array.new (1,2) , ruby … Read more
Whitespace in the format string matches 0 or more whitespace characters in the input. So “%d c %d” expects number, then any amount of whitespace characters, then character c, then any amount of whitespace characters and another number at the end. “%dc%d” expects number, c, number. Also note, that if you use * in the … Read more