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

javascript - String Split not working - Stack Overflow

programmeradmin1浏览0评论

I'm having a problem with splitting strings in Javascript in Max/MSP.

outlet is the Max/MSP version of printf etc.

The string splits weirdly, but it seems to only output both words comma seperated.

function sample_callback(args)  // Callback
{
    var keyword=args;
    var trackname=keyword.toString().split(" ");
    var name = trackname[0]; // trackname[1] outputs nothing.
    outlet(0, name);
}

Any help is greatly received.

I'm having a problem with splitting strings in Javascript in Max/MSP.

outlet is the Max/MSP version of printf etc.

The string splits weirdly, but it seems to only output both words comma seperated.

function sample_callback(args)  // Callback
{
    var keyword=args;
    var trackname=keyword.toString().split(" ");
    var name = trackname[0]; // trackname[1] outputs nothing.
    outlet(0, name);
}

Any help is greatly received.

Share Improve this question edited Nov 28, 2012 at 16:09 Adam asked Nov 28, 2012 at 15:46 AdamAdam 4931 gold badge5 silver badges16 bronze badges 5
  • I suspect that the code calling the callback, sample_callback, isn't binding correctly, but no way to tell w/o that call. Can you include some of the code that calls sample_callback? – mrk Commented Nov 28, 2012 at 15:49
  • Please show us how you're calling sample_callback, and the definition of outlet. BTW: if you're coming from a C/C++ background, bear in mind that JS has more in common with Scheme/Lisp, so treat functions as objects (pass them as arguments/return values at will) – Elias Van Ootegem Commented Nov 28, 2012 at 15:51
  • I've realised a mistake but it still doesn't work - I've edited the question. Thanks for you're quick reply so far. – Adam Commented Nov 28, 2012 at 15:59
  • 8 What is the value returned from keyword.toString()? – Aaron Kurtzhals Commented Nov 28, 2012 at 16:07
  • It returned the words comma separated instead of space. It must have converted it during the toString function. Thank you @Aaron Kurtzhals – Adam Commented Nov 28, 2012 at 16:12
Add a comment  | 

2 Answers 2

Reset to default 12

Big thanks to Aaron Kurtzhals . Hopefully the upvote in the comment counts towards your rep!

A simple overlooked checking of what the string is helped me out. oops. The working code is now..

function sample_callback(args)  // Callback
{
  var keyword=args.toString();
  var trackname=keyword.split(",");
  var name = trackname[0];
  outlet(0, name);
}

Cheers

function sample_callback(args)  // Callback
{
    var keyword=args.toString()`enter code here`;
    var trackname=keyword.toString().split(" ");
    var name = trackname[0]; // trackname[1] outputs nothing.
    outlet(0, name);
}
发布评论

评论列表(0)

  1. 暂无评论