Nodejs String Split splitStr(delimiter)

Here you can find the source of splitStr(delimiter)

Method Source Code

/**//from  w w  w.  j  a v a2 s.  c om
 * Reverse Words In A String Using JavaScript
 *
 * @Reference:
 * https://blog.nraboy.com/2015/02/reverse-words-string-using-javascript/
 */

function reverseWords(str) {
  var result = "";
  var wordArray = str.split(" ");
  for (var i = wordArray.length - 1; i >= 0; i--) {
    result += wordArray[i] + " ";
  }
  return result.trim(); // To remove trailing spaces.
}

// If we had to write out own split() function and not use the in-built one.
String.prototype.splitStr = function (delimiter) {
  var stringArray = [];
  var tempStr = "";
  for (var i = 0; i < this.length; i++) {
    if (this.charAt(i) === delimiter) {
      stringArray.push(tempStr);
      tempStr = "";
    } else {
      tempStr += this.charAt(i);
    }
  }
  if (tempStr !== "") {
    stringArray.push(tempStr);
  }
  return stringArray;
};

Related

  1. 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);
    ...
    
  2. splitLength(size)
    String.prototype.splitLength = function(size) {
        var regex = new RegExp('.{1,' + size + '}', 'g');
        return this.match(regex);
    };
    
  3. 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;
    ...
    
  4. 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;
    ...
    
  5. splitOnce(regex)
    String.prototype.splitOnce = function (regex) {
        var match = this.match(regex);
        if (match) {
            var match_i = this.indexOf(match[0]);
            return [this.substring(0, match_i),
            this.substring(match_i + match[0].length)];
        } else {
            return [this, ""];
    
  6. splitWord()
    String.prototype.splitWord = function () {
      return this.split(/(?=[A-Z])/).join(' ');
    };
    
  7. splitchunks(s)
    String.prototype.splitchunks = function(s) {
      return s.split(/\n\n+/) 
    
  8. splitenum(separator)
    String.prototype.splitenum = function(separator){
        var tokens = this.split(separator);
        var obj = {}
        for (var i=0; i< tokens.length; i++ ){
            obj[i] = tokens[i];
        return obj;
    
  9. splitlines(s)
    String.prototype.splitlines = function(s) {
      return s.split('\n')