javascript - How can I check if the contents of a variable start with something? - Stack Overflow

I would like to do something like this:if (idCity == 'AB*') {do something}In other words

I would like to do something like this:

if (idCity == 'AB*') {
   //  do something
}

In other words. I want to check that idCity starts with the string "AB". How can I do this in Javascript?

I would like to do something like this:

if (idCity == 'AB*') {
   //  do something
}

In other words. I want to check that idCity starts with the string "AB". How can I do this in Javascript?

Share Improve this question asked Jan 8, 2012 at 8:06 Samantha J T StarSamantha J T Star 33k89 gold badges257 silver badges441 bronze badges 3
  • See stackoverflow./questions/646628/javascript-startswith – Joachim Isaksson Commented Jan 8, 2012 at 8:13
  • possible duplicate of javascript - check if string begins with something? – Felix Kling Commented Jan 8, 2012 at 9:10
  • How about reading through the list of string manipulation methods until you find one that seems to do the trick? – nnnnnn Commented Jan 8, 2012 at 9:20
Add a ment  | 

4 Answers 4

Reset to default 3
if (idCity.substr(0,2) == 'AB') {
   alert('the string starts with AB');
}
if(idCity.indexOf('AB') == 0)
{
  alert('the string starts with AB');
}
if(idCity.substr(0,2)=='AB'){
}

If 'AB' is not constant string, you may use

if(idCity.substr(0,start.length)==start){
}
idCity = 'AB767 something';

function startWith(searchString){
    if (idCity.indexOf(searchString) == 0){
         return true;
    }
    return false;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745143123a4613520.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信