Languages
[Edit]
DE

JavaScript - wie verwendet man Symbol.split Methode?

4 points
Created by:
Nikki
10520

Mit JavaScript ist es möglich, String mit Symbol.split (mit Splitter) auf folgende Art und Weise zu teilen.

1. String mit Symbol.split Methode (mit Splitter) - Beispiel

// ONLINE-RUNNER:browser;

var splitter = {
    [Symbol.split] : function(text, limit) { // Diese Funktion teilt den String nach Leerzeichen auf 
        let result = [ ];

        if (text.length > 0) {
            let index = 0;
    
            for (let i = 0; i < text.length; ++i) {
                if(text[i] == ' ') {
                    result.push(text.substring(index, i));
    
                    index = i + 1;
                }
            }

            result.push(text.substring(index));
        }

        return result;
    }
};

// Beispiel:

var text = 'This is some text...';
var array = text.split(splitter);

console.log(array);

Ausgabe (mit NodeJS):

[ 'This', 'is', 'some', 'text...' ]

Hinweis: Symbol.split wurde in ES2015 eingeführt.

Siehe auch

  1. JavaScript - wie kann man einen String teilen?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join