contains
Checks if a string contains a substring.
contains
Signature
typescript
function contains(str: string, substring: string, ignoreCase: boolean = false): booleanParameters
str- The string to search insubstring- The substring to search forignoreCase- If true, case-insensitive search (default: false)
Returns
true if the substring is found, false otherwise.
Examples
typescript
import { contains } from 'strio'
// Basic usage
contains('hello world', 'world')
// true
contains('hello world', 'foo')
// false
// Case sensitive (default)
contains('Hello World', 'world')
// false
// Case insensitive
contains('Hello World', 'world', false)
// true
// Empty substring
contains('hello', '')
// true
// At the beginning
contains('hello world', 'hello')
// true
// At the end
contains('hello world', 'world')
// trueUse Cases
typescript
import { contains } from 'strio'
// Search functionality
function searchProducts(products: string[], query: string): string[] {
return products.filter(p => contains(p.toLowerCase(), query.toLowerCase()))
}
// Validation
function hasProhibitedWords(text: string, words: string[]): boolean {
return words.some(word => contains(text.toLowerCase(), word.toLowerCase()))
}
// Feature detection
function hasFeature(features: string, feature: string): boolean {
return contains(features, feature, false)
}Related Functions
- containsAll - Check if contains all substrings
- containsAny - Check if contains any substring
- count - Count occurrences