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

后端 未结 19 1763
我在风中等你
我在风中等你 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 22:54

    Also check out underscore.string.js. It comes with a bunch of useful string testing and manipulation methods, including a startsWith method. From the docs:

    startsWith _.startsWith(string, starts)

    This method checks whether string starts with starts.

    _("image.gif").startsWith("image")
    => true
    

提交回复
热议问题