Implementing custom function like String.prototype.split() function in JavaScript

We need to create a custom function that behaves like the built-in String.prototype.split() method. This function will split a string into an array based on a separator character or string.

Problem Statement

We are required to write a JavaScript function that extends the String prototype. The function should take a string separator as an argument and return an array of parts where the original string is split by that separator.

Implementation

Here's how to implement a custom split function:

String.prototype.customSplit = function(sep = '') {
    const res = [];
    let temp = '';
    
    // Handle empty separator case
    if (sep === '') {
        for (let i = 0; i < this.length; i++) {
            res.push(this[i]);
        }
        return res;
    }
    
    for (let i = 0; i < this.length; i++) {
        const el = this[i];
        
        if (el === sep) {
            res.push(temp);
            temp = '';
        } else {
            temp += el;
        }
    }
    
    // Push the last part
    if (temp || this.length === 0) {
        res.push(temp);
    }
    
    return res;
};

// Test the custom split function
const str = 'this is some string';
console.log(str.customSplit(' '));
console.log(str.customSplit('i'));
console.log(str.customSplit(''));
[ 'this', 'is', 'some', 'string' ]
[ 'th', 's ', 's some str', 'ng' ]
[ 't', 'h', 'i', 's', ' ', 'i', 's', ' ', 's', 'o', 'm', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g' ]

Key Points

  • Use function declaration instead of arrow function to access this context
  • Handle empty separator case by returning individual characters
  • Build temporary string until separator is found
  • Always push the final accumulated string to results

Comparison with Built-in split()

Feature Built-in split() Custom split()
Basic splitting Yes Yes
Empty separator Returns characters Returns characters
Limit parameter Supported Not implemented
Regex support Supported Not implemented

Enhanced Version with Limit Support

String.prototype.customSplitWithLimit = function(sep = '', limit) {
    const res = [];
    let temp = '';
    
    if (sep === '') {
        for (let i = 0; i < this.length; i++) {
            if (limit && res.length >= limit) break;
            res.push(this[i]);
        }
        return res;
    }
    
    for (let i = 0; i < this.length; i++) {
        if (limit && res.length >= limit - 1) {
            temp += this[i];
            continue;
        }
        
        const el = this[i];
        if (el === sep) {
            res.push(temp);
            temp = '';
        } else {
            temp += el;
        }
    }
    
    if (temp || this.length === 0) {
        res.push(temp);
    }
    
    return res;
};

const testStr = 'apple,banana,cherry,date';
console.log(testStr.customSplitWithLimit(',', 2));
console.log(testStr.customSplitWithLimit(',', 3));
[ 'apple', 'banana,cherry,date' ]
[ 'apple', 'banana', 'cherry,date' ]

Conclusion

Creating a custom split function demonstrates string manipulation and prototype extension in JavaScript. Use regular functions instead of arrow functions when extending prototypes to maintain proper this context.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements