What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods? String.prototype.slice String.prototype.substring 8 s 8 slice() works like substring() with a few different behaviors. Syntax: string.slice(start, stop); Syntax: string.substring(start, stop); What they have in common: If start equals stop: returns an empty string If stop is omitted: extracts characters to the end of the … Read more

How do I check if string contains substring? [duplicate]

This question already has answers here: How to check whether a string contains a substring in JavaScript? (3 answers) Closed 4 years ago. I have a shopping cart that displays product options in a dropdown menu and if they select “yes”, I want to make some other fields on the page visible. The problem is … Read more

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

How can I check if a string (NSString) contains another smaller string? I was hoping for something like: NSString *string = @”hello bla bla”; NSLog(@”%d”,[string containsSubstring:@”hello”]); But the closest I could find was: if ([string rangeOfString:@”hello”] == 0) { NSLog(@”sub string doesnt exist”); } else { NSLog(@”exists”); } Anyway, is that the best way to … Read more

How do I check if a string contains a specific word?

This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. Consider: $a=”How are you?”; if ($a contains ‘are’) echo ‘true’; Suppose I have the code above, what is the correct way to write the statement if ($a contains ‘are’)? 3 36

How to check whether a string contains a substring in JavaScript?

This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. Usually I would expect a String.contains() method, but there doesn’t seem to be one. What is a reasonable way to check for this? 3 ECMAScript 6 introduced String.prototype.includes: const string = “foo”; const … Read more

What is the difference between String.subString() and String.subSequence()

Using str.subSequence(begin, end) returns a CharSequence which is a read-only form of the string represented as a sequence of chars. For example: String string = “Hello”; CharSequence subSequence = string.subSequence(0, 5); It’s read only in the sense that you can’t change the chars within the CharSequence without instantiating a new instance of a CharSequence. If you have to use str.subSequence(begin, end), you can cast the … Read more