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

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

    var str = 'hol';
    var data = 'hola mundo';
    if (data.length >= str.length && data.substring(0, str.length) == str)
        return true;
    else
        return false;
    

提交回复
热议问题