Cocoa – Trim all leading whitespace from NSString

This creates an NSString category to do what you need. With this, you can call NSString *newString = [mystring stringByTrimmingLeadingWhitespace]; to get a copy minus leading whitespace. (Code is untested, may require some minor debugging.) @interface NSString (trimLeadingWhitespace) -(NSString*)stringByTrimmingLeadingWhitespace; @end @implementation NSString (trimLeadingWhitespace) -(NSString*)stringByTrimmingLeadingWhitespace { NSInteger i = 0; while ((i < [self length]) && … Read more

Trim trailing spaces with PostgreSQL

There are many different invisible characters. Many of them have the property WSpace=Y (“whitespace”) in Unicode. But some special characters are not considered “whitespace” and still have no visible representation. The excellent Wikipedia articles about space (punctuation) and whitespace characters should give you an idea. <rant>Unicode sucks in this regard: introducing lots of exotic characters … Read more

How to remove only trailing spaces of a string in Java and keep leading spaces?

Since JDK 11 If you are on JDK 11 or higher you should probably be using stripTrailing(). Earlier JDK versions Using the regular expression \s++$, you can replace all trailing space characters (includes space and tab characters) with the empty string (“”). final String text = ” foo “; System.out.println(text.replaceFirst(“\\s++$”, “”)); Output foo Online demo. … Read more

How to select first 10 words of a sentence?

implode(‘ ‘, array_slice(explode(‘ ‘, $sentence), 0, 10)); To add support for other word breaks like commas and dashes, preg_match gives a quick way and doesn’t require splitting the string: function get_words($sentence, $count = 10) { preg_match(“/(?:\w+(?:\W+|$)){0,$count}/”, $sentence, $matches); return $matches[0]; } As Pebbl mentions, PHP doesn’t handle UTF-8 or Unicode all that well, so if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)