Function startsWith

  • Checks if a string contains another string at the beginning of the string.

    Checks if one string startsWith another string. Optional position parameter to start searching from a certain index.

    Parameters

    • str: string

      The string that might contain the target string.

    • target: string

      The string to search for.

    • Optionalposition: number

      An optional offset to start searching in the str string

    Returns boolean

    • True if the str string starts with the target string.
    const isPrefix = startsWith('fooBar', 'foo') // returns true
    const isPrefix = startsWith('fooBar', 'bar') // returns false
    const isPrefix = startsWith('fooBar', 'abc') // returns false
    const isPrefix = startsWith('fooBar', 'Bar', 2) // returns true
    const isPrefix = startsWith('fooBar', 'Bar', 5) // returns false