最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert Object to String - Stack Overflow

programmeradmin1浏览0评论

I've got an object returned from a field msg which is an object. I'm attempted to get the value from msg and convert it into a string so I can use .startswith(). I'm trying the following..

 var msgstring = msg.value
 if(msgstring.startsWith("string")){
    //Doing stuff!
 }

However, I get the following error...

Uncaught TypeError: Object string here has no method 'startsWith'

Where am I going wrong?

I've got an object returned from a field msg which is an object. I'm attempted to get the value from msg and convert it into a string so I can use .startswith(). I'm trying the following..

 var msgstring = msg.value
 if(msgstring.startsWith("string")){
    //Doing stuff!
 }

However, I get the following error...

Uncaught TypeError: Object string here has no method 'startsWith'

Where am I going wrong?

Share Improve this question asked Mar 15, 2011 at 12:17 SkizitSkizit 44.9k93 gold badges213 silver badges270 bronze badges 2
  • 1 You have a missing ) indicating that this isn't your real testcase. – Lightness Races in Orbit Commented Mar 15, 2011 at 12:21
  • Possible duplicate of stackoverflow.com/questions/646628/javascript-startswith – Chris Commented Mar 15, 2011 at 12:24
Add a comment  | 

7 Answers 7

Reset to default 16

Javascript has no startsWith method. You can use

msgstring.indexOf('string') === 0

The error is correct, JS has no native startsWith method of string object.

You can build it yourself extending the prototype, or use function:

function StartsWith(s1, s2) {
  return (s1.length >= s2.length && s1.substr(0, s2.length) == s2);
}

var msgstring = msg.value;
if(StartsWith(msgstring, "string") {
    //Doing stuff!
 }

You are going wrong at assuming that there is a function called "startsWith". There is no such function in JavaScript.

Please have a look at this question:

How to check if a string "StartsWith" another string?

You will find there how you cann add this function to Javascript objects yourself.

There is no startsWith() function in JavaScript. You need to write one yourself.

Exactly what you're reading, the string object (your variable msgstring) does not have a method called startsWith. Read more on using strings in here.

You probably want to do something like:

msgstring.substr(0, 6) == "string"

Try this:

var msgstring = msg.value;
 if(!msgstring.indexOf("string")){     
         //Doing stuff! 
 }

As everybody has already mentioned that there is no startsWith function available with JS and we need to create a one for us. Below is the implementation for the same

if (typeof String.prototype.startsWith != 'function') {
  //Implementation to startsWith starts below
  String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
  };
}

after doing this you can call the startsWith function directly with your string. this keyword will be the string on which you have called the function and str will be the string you are comparing with.

发布评论

评论列表(0)

  1. 暂无评论