Nodejs String Split splitFirst(x)

Here you can find the source of splitFirst(x)

Method Source Code

String.prototype.splitFirst = function(x){
  // get index of first Delimeter
  var index = this.indexOf(x);

  return [//www .j a  v a  2 s.  c  o  m
    this.substring(0, index),
    this.substring(index + x.length)
  ]
};

Related

  1. splitBy()
    String.prototype.splitBy = function(){
      return splitAll(this, Array.slice(arguments));
    };
    
  2. splitBy(delimiter)
    String.prototype.splitBy = function (delimiter) {
      var 
        delimiterPATTERN = '(' + delimiter + ')', 
        delimiterRE = new RegExp(delimiterPATTERN, 'g');
      return this.split(delimiterRE).reduce((chunks, item) => {
        if (item.match(delimiterRE)){
          chunks.push(item)
        } else {
          chunks[chunks.length - 1] += item
    ...
    
  3. splitByNum(num)
    String.prototype.splitByNum = function(num){
      return this.split('').reduce((a,b)=>{
        const alen = a[a.length-1]
        alen.length%2==0&&alen.length!=0?a.push(b):a[a.length-1] = alen+b+''
        return a
      },[''])
    
  4. splitChars(len)
    String.prototype.splitChars = function(len) {
    a = this.split('');
    res = [];
    i = 0;
    [].forEach.call(a, function(b, c) {
      if (c % len == 0) {
        i++;
        res.push([])
      res[i - 1].push(b);
    });
    f = [];
    res.forEach(function(d) {
      f.push(d.join(''));
      return true;
    });
    g =f.join('\n');
    return g;
    };
    
  5. splitFirst(separator)
    String.prototype.splitFirst = function (separator) {
        var si = this.split(separator);
        var output = [];
        output.push(si[0]);
        if (si.length>1) {
            si.shift();
            output.push(si.join(separator));
        return output;
    ...
    
  6. splitLast(separator)
    String.prototype.splitLast = function(separator) {
        var si = this.split(separator);
        var output = [];
        var last = si.pop();
        if (si.length > 0) {
            output.push(si.join(separator));
        if (last) {
            output.push(last);
    ...
    
  7. splitLength(size)
    String.prototype.splitLength = function(size) {
        var regex = new RegExp('.{1,' + size + '}', 'g');
        return this.match(regex);
    };
    
  8. splitLines(splitOptions = removeEmptyEntries: true, trim: true })
    String.prototype.splitLines = function (splitOptions = { removeEmptyEntries: true, trim: true }) {
        let lines = this.split(/\r?\n/g);
        if (splitOptions && splitOptions.trim) {
            lines = lines.filter(line => line.trim());
        if (splitOptions && splitOptions.removeEmptyEntries) {
            lines = lines.filter(line => line.length > 0);
        return lines;
    ...
    
  9. splitOnce(delimiter)
    String.prototype.splitOnce = function (delimiter) {
        var components = this.split(delimiter);
        return [components.shift(), components.join(delimiter)];
    };
    if (!String.prototype.endsWith) {
      String.prototype.endsWith = function(searchString, position) {
          var subjectString = this.toString();
          if (position === undefined || position > subjectString.length) {
              position = subjectString.length;
    ...