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

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

    I just learned about this string library:

    http://stringjs.com/

    Include the js file and then use the S variable like this:

    S('hi there').endsWith('hi there')
    

    It can also be used in NodeJS by installing it:

    npm install string
    

    Then requiring it as the S variable:

    var S = require('string');
    

    The web page also has links to alternate string libraries, if this one doesn't take your fancy.

提交回复
热议问题