JS: mytok

string manipulation

JavaScript

// always left-to-right scan, space-sparated string
// option: cut, rest
// motivated, but not the same as:
// url:
// http://stackoverflow.com/questions/7841711/javascript-or-jquery-equivalent-of-phps-strtok

/**
**  example:
**  "a" = mytok ("a b c d e", "cut");
**  "b c d e" = mytok ("a b c d e", "rest");
*/
function mytok (s, option)
{
	var separator = " ";
	var n = s.indexOf (separator);
	var read = s.substr (0,n);
	
    if (option === 'cut')
        return read;
    else {
	    s = s.substr (++n);  // the rest
    	return s;
    }
}

var ss = "https://www.youtube.com/watch?v=dQw4w9WgXcQ asdasd";
var s;

alert (mytok (ss, "cut"));
alert (mytok (ss, "rest"));