How do I check if a string contains another string in Objective-C?

NSString *string = @”hello bla bla”; if ([string rangeOfString:@”bla”].location == NSNotFound) { NSLog(@”string does not contain bla”); } else { NSLog(@”string contains bla!”); } The key is noticing that rangeOfString: returns an NSRange struct, and the documentation says that it returns the struct {NSNotFound, 0} if the “haystack” does not contain the “needle”. And if … Read more

Why does comparing strings using either ‘==’ or ‘is’ sometimes produce a different result?

is is identity testing, == is equality testing. what happens in your code would be emulated in the interpreter like this: >>> a=”pub” >>> b = ”.join([‘p’, ‘u’, ‘b’]) >>> a == b True >>> a is b False so, no wonder they’re not the same, right? In other words: a is b is the … Read more

Multiline string literal in C#

You can use the @ symbol in front of a string to form a verbatim string literal: string query = @”SELECT foo, bar FROM table WHERE id = 42″; You also do not have to escape special characters when you use this method, except for double quotes as shown in Jon Skeet’s answer.

How do I trim whitespace from a string?

To remove all whitespace surrounding a string, use .strip(). Examples: >>> ‘ Hello ‘.strip() ‘Hello’ >>> ‘ Hello’.strip() ‘Hello’ >>> ‘Bob has a cat’.strip() ‘Bob has a cat’ >>> ‘ Hello ‘.strip() # ALL consecutive spaces at both ends removed ‘Hello’ Note that str.strip() removes all whitespace characters, including tabs and newlines. To remove only … Read more

Trim string in JavaScript

All browsers since IE9+ have trim() method for strings: ” \n test \n “.trim(); // returns “test” here For those browsers who does not support trim(), you can use this polyfill from MDN: if (!String.prototype.trim) { (function() { // Make sure we trim BOM and NBSP var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; String.prototype.trim = function() { return … Read more

How do I get the filename without the extension from a path in Python?

Getting the name of the file without the extension: import os print(os.path.splitext(“/path/to/some/file.txt”)[0]) Prints: /path/to/some/file Documentation for os.path.splitext. Important Note: If the filename has multiple dots, only the extension after the last one is removed. For example: import os print(os.path.splitext(“/path/to/some/file.txt.zip.asc”)[0]) Prints: /path/to/some/file.txt.zip See other answers below if you need to handle that case.

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