How to check if a string “StartsWith” another string?

后端 未结 19 1305
我在风中等你
我在风中等你 2020-11-21 22:35

How would I write the equivalent of C#\'s String.StartsWith in JavaScript?

var haystack = \'hello world\';
var needle = \'he\';

haystack.startsWith(needle)          


        
19条回答
  •  情书的邮戳
    2020-11-21 23:04

    The string object has methods like startsWith, endsWith and includes methods.

    • StartsWith checks whether the given string starts at the beginning or not.

    • endsWith checks whether the given string is at the end or not.

    • includes checks whether the given string is present at any part or not.

    You can find the complete difference between these three in the bellow youtube video

    https://www.youtube.com/watch?v=E-hyeSwg0PA

提交回复
热议问题