Dividi la stringa di grandi dimensioni in blocchi di dimensioni n in JavaScript


209

Vorrei dividere una stringa molto grande (diciamo 10.000 caratteri) in blocchi di dimensioni N.

Quale sarebbe il modo migliore in termini di prestazioni per farlo?

Ad esempio: "1234567890"diviso per 2 diventerebbe ["12", "34", "56", "78", "90"].

Sarebbe possibile utilizzare qualcosa del genere String.prototype.matche, in tal caso, sarebbe il modo migliore per farlo in termini di prestazioni?

Risposte:


457

Puoi fare qualcosa del genere:

"1234567890".match(/.{1,2}/g);
// Results in:
["12", "34", "56", "78", "90"]

Il metodo continuerà a funzionare con stringhe la cui dimensione non è un multiplo esatto della dimensione del blocco:

"123456789".match(/.{1,2}/g);
// Results in:
["12", "34", "56", "78", "9"]

In generale, per qualsiasi stringa da cui si desidera estrarre al massimo sottostringhe di dimensioni n , è necessario:

str.match(/.{1,n}/g); // Replace n with the size of the substring

Se la tua stringa può contenere newline o ritorni a capo, dovresti:

str.match(/(.|[\r\n]){1,n}/g); // Replace n with the size of the substring

Per quanto riguarda le prestazioni, l'ho provato con circa 10k caratteri e ci è voluto poco più di un secondo su Chrome. YMMV.

Questo può anche essere usato in una funzione riutilizzabile:

function chunkString(str, length) {
  return str.match(new RegExp('.{1,' + length + '}', 'g'));
}

8
Poiché questa risposta ha ormai quasi 3 anni, ho voluto provare di nuovo il test delle prestazioni eseguito da @Vivin. Quindi FYI, dividere 100k caratteri due per due usando il regex dato è istantaneo su Chrome v33.
aymericbeaumet

1
@Fmstrat Cosa intendi con "se la tua stringa contiene spazi, non conta nella lunghezza"? Sì, .non corrisponde affatto a Newline. Aggiornerò la risposta in modo che prenda \ne \rin considerazione.
Vivin Paliath,

2
Qualcosa del genere var chunks = str.split("").reverse().join().match(/.{1, 4}/).map(function(s) { return s.split("").reverse().join(); });. Lo fa a pezzi di 4. Non sono sicuro di cosa intendi per "meno o più". Tieni presente che questo non funzionerà in generale, specialmente con stringhe che contengono caratteri combinati e possono anche spezzare le stringhe Unicode.
Vivin Paliath,

2
Secondo developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… puoi abbinare qualsiasi carattere, comprese le nuove linee, con [^]. Con questo il tuo esempio si tradurrebbe instr.match(/[^]{1,n}/g)
Francesc Rosas il

1
Per chiunque sia alla ricerca di blocchi di corde veramente veloci con benchmark di performance su jsperf, vedere la mia risposta . L'uso di un regex è il metodo di chunking più lento di tutti.
Justin Warkentin,

34

Ho creato diverse varianti più veloci che puoi vedere su jsPerf . Il mio preferito è questo:

function chunkSubstr(str, size) {
  const numChunks = Math.ceil(str.length / size)
  const chunks = new Array(numChunks)

  for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
    chunks[i] = str.substr(o, size)
  }

  return chunks
}

2
quindi questo ha funzionato favolosamente su stringhe lunghe (circa 800k - 9m caratteri) tranne quando ho impostato la dimensione su 20 per qualche motivo l'ultimo pezzo non è stato restituito ... comportamento molto strano.
David,

1
@DavidAnderton Buona cattura. L'ho risolto e, a quanto pare, sembra funzionare ancora più velocemente. Stava arrotondando quando avrebbe dovuto fare Math.ceil()per determinare il numero corretto di blocchi.
Justin Warkentin,

1
Grazie! L'ho messo insieme come modulo NPM con supporto Unicode opzionale - github.com/vladgolubev/fast-chunk-string
Vlad Holubiev

33

Linea di fondo:

  • matchè molto inefficiente, sliceè meglio, su Firefox substr/ substringè ancora meglio
  • match è ancora più inefficiente per stringhe brevi (anche con regex memorizzato nella cache - probabilmente a causa del tempo di installazione dell'analisi regex)
  • match è ancora più inefficiente per le grandi dimensioni del pezzo (probabilmente a causa dell'incapacità di "saltare")
  • per stringhe più lunghe con dimensioni del blocco molto ridotte, matchsupera le prestazioni di sliceIE precedenti ma perde comunque su tutti gli altri sistemi
  • rocce jsperf

19

Questa è una soluzione semplice e veloce -

function chunkString (str, len) {
  const size = Math.ceil(str.length/len)
  const r = Array(size)
  let offset = 0
  
  for (let i = 0; i < size; i++) {
    r[i] = str.substr(offset, len)
    offset += len
  }
  
  return r
}

console.log(chunkString("helloworld", 3))
// => [ "hel", "low", "orl", "d" ]

// 10,000 char string
const bigString = "helloworld".repeat(1000)
console.time("perf")
const result = chunkString(bigString, 3)
console.timeEnd("perf")
console.log(result)
// => perf: 0.385 ms
// => [ "hel", "low", "orl", "dhe", "llo", "wor", ... ]


1
Devi usare substr()invece di substring().
Leif

2
Sono curioso, perché i trattini bassi nei nomi delle variabili?
Felipe Valdes,

@FelipeValdes Presumo di non confonderli con variabili globali / di parametri o di indicarli come di ambito privato.
Mr. Polywhirl,

15

Sorpresa! Puoi usare la divisione per dividere.

var parts = "1234567890 ".split(/(.{2})/).filter(O=>O)

Risultati in [ '12', '34', '56', '78', '90', ' ' ]


Questa è la risposta meravigliosa.
Galkin,

2
A cosa serve filter (o=>o)?
Ben Carp,

2
regex corrente crea elementi di array vuoti tra blocchi. filter(x=>x)è usato per filtrare quegli elementi vuoti
artemdev,

Breve e intelligente, ma scorre più volte sull'input. Questa risposta è oltre 4 volte più lenta di altre soluzioni in questo thread.
Grazie

6
@BenCarp È l'operatore motociclistico. Lo fa andare più veloce. ;)
Fozi,

7
var str = "123456789";
var chunks = [];
var chunkSize = 2;

while (str) {
    if (str.length < chunkSize) {
        chunks.push(str);
        break;
    }
    else {
        chunks.push(str.substr(0, chunkSize));
        str = str.substr(chunkSize);
    }
}

alert(chunks); // chunks == 12,34,56,78,9

5

Ho scritto una funzione estesa, quindi la lunghezza del blocco può anche essere una matrice di numeri, come [1,3]

String.prototype.chunkString = function(len) {
    var _ret;
    if (this.length < 1) {
        return [];
    }
    if (typeof len === 'number' && len > 0) {
        var _size = Math.ceil(this.length / len), _offset = 0;
        _ret = new Array(_size);
        for (var _i = 0; _i < _size; _i++) {
            _ret[_i] = this.substring(_offset, _offset = _offset + len);
        }
    }
    else if (typeof len === 'object' && len.length) {
        var n = 0, l = this.length, chunk, that = this;
        _ret = [];
        do {
            len.forEach(function(o) {
                chunk = that.substring(n, n + o);
                if (chunk !== '') {
                    _ret.push(chunk);
                    n += chunk.length;
                }
            });
            if (n === 0) {
                return undefined; // prevent an endless loop when len = [0]
            }
        } while (n < l);
    }
    return _ret;
};

Il codice

"1234567890123".chunkString([1,3])

sarà di ritorno:

[ '1', '234', '5', '678', '9', '012', '3' ]

4

Divide la stringa grande in Stringhe piccole di parole date .

function chunkSubstr(str, words) {
  var parts = str.split(" ") , values = [] , i = 0 , tmpVar = "";
  $.each(parts, function(index, value) {
      if(tmpVar.length < words){
          tmpVar += " " + value;
      }else{
          values[i] = tmpVar.replace(/\s+/g, " ");
          i++;
          tmpVar = value;
      }
  });
  if(values.length < 1 &&  parts.length > 0){
      values[0] = tmpVar;
  }
  return values;
}

3
var l = str.length, lc = 0, chunks = [], c = 0, chunkSize = 2;
for (; lc < l; c++) {
  chunks[c] = str.slice(lc, lc += chunkSize);
}

2

Vorrei usare un regex ...

var chunkStr = function(str, chunkLength) {
    return str.match(new RegExp('[\\s\\S]{1,' + +chunkLength + '}', 'g'));
}

1
const getChunksFromString = (str, chunkSize) => {
    var regexChunk = new RegExp(`.{1,${chunkSize}}`, 'g')   // '.' represents any character
    return str.match(regexChunk)
}

Chiamalo come necessario

console.log(getChunksFromString("Hello world", 3))   // ["Hel", "lo ", "wor", "ld"]

1

Ecco una soluzione che mi è venuta in mente per le stringhe di template dopo un piccolo esperimento:

Uso:

chunkString(5)`testing123`

function chunkString(nSize) {
    return (strToChunk) => {
        let result = [];
        let chars = String(strToChunk).split('');

        for(let i = 0; i < (String(strToChunk).length / nSize); i++) {
            result = result.concat(chars.slice(i*nSize,(i+1)*nSize).join(''));
        }
        return result
    }
}

document.write(chunkString(5)`testing123`);
// returns: testi,ng123

document.write(chunkString(3)`testing123`);
// returns: tes,tin,g12,3


1

Puoi usare reduce()senza regex:

(str, n) => {
  return str.split('').reduce(
    (acc, rec, index) => {
      return ((index % n) || !(index)) ? acc.concat(rec) : acc.concat(',', rec)
    },
    ''
  ).split(',')
}

Penso che sarà di grande aiuto se tu fornissi esempi su come usare il tuo reducemetodo.
kiatng

0

Sotto forma di una funzione prototipo:

String.prototype.lsplit = function(){
    return this.match(new RegExp('.{1,'+ ((arguments.length==1)?(isFinite(String(arguments[0]).trim())?arguments[0]:false):1) +'}', 'g'));
}

0

Ecco il codice che sto usando, usa String.prototype.slice .

Sì, è abbastanza lungo quando arriva una risposta mentre cerca di seguire gli standard attuali il più vicino possibile e ovviamente contiene una quantità ragionevole di commenti JSDOC . Tuttavia, una volta minimizzato, il codice è solo 828 byte e una volta compresso per la trasmissione è solo 497 byte.

Il 1 metodo a cui questo si aggiunge String.prototype(utilizzando Object.defineProperty ove disponibile) è:

  1. toChunks

Sono stati inclusi numerosi test per verificare la funzionalità.

Preoccupato che la lunghezza del codice influenzerà le prestazioni? Non c'è bisogno di preoccuparsi, http://jsperf.com/chunk-string/3

Gran parte del codice aggiuntivo è lì per essere sicuro che il codice risponderà allo stesso in più ambienti javascript.

/*jslint maxlen:80, browser:true, devel:true */

/*
 * Properties used by toChunks.
 */

/*property
    MAX_SAFE_INTEGER, abs, ceil, configurable, defineProperty, enumerable,
    floor, length, max, min, pow, prototype, slice, toChunks, value,
    writable
*/

/*
 * Properties used in the testing of toChunks implimentation.
 */

/*property
    appendChild, createTextNode, floor, fromCharCode, getElementById, length,
    log, pow, push, random, toChunks
*/

(function () {
    'use strict';

    var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;

    /**
     * Defines a new property directly on an object, or modifies an existing
     * property on an object, and returns the object.
     *
     * @private
     * @function
     * @param {Object} object
     * @param {string} property
     * @param {Object} descriptor
     * @return {Object}
     * @see https://goo.gl/CZnEqg
     */
    function $defineProperty(object, property, descriptor) {
        if (Object.defineProperty) {
            Object.defineProperty(object, property, descriptor);
        } else {
            object[property] = descriptor.value;
        }

        return object;
    }

    /**
     * Returns true if the operands are strictly equal with no type conversion.
     *
     * @private
     * @function
     * @param {*} a
     * @param {*} b
     * @return {boolean}
     * @see http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.4
     */
    function $strictEqual(a, b) {
        return a === b;
    }

    /**
     * Returns true if the operand inputArg is undefined.
     *
     * @private
     * @function
     * @param {*} inputArg
     * @return {boolean}
     */
    function $isUndefined(inputArg) {
        return $strictEqual(typeof inputArg, 'undefined');
    }

    /**
     * The abstract operation throws an error if its argument is a value that
     * cannot be converted to an Object, otherwise returns the argument.
     *
     * @private
     * @function
     * @param {*} inputArg The object to be tested.
     * @throws {TypeError} If inputArg is null or undefined.
     * @return {*} The inputArg if coercible.
     * @see https://goo.gl/5GcmVq
     */
    function $requireObjectCoercible(inputArg) {
        var errStr;

        if (inputArg === null || $isUndefined(inputArg)) {
            errStr = 'Cannot convert argument to object: ' + inputArg;
            throw new TypeError(errStr);
        }

        return inputArg;
    }

    /**
     * The abstract operation converts its argument to a value of type string
     *
     * @private
     * @function
     * @param {*} inputArg
     * @return {string}
     * @see https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tostring
     */
    function $toString(inputArg) {
        var type,
            val;

        if (inputArg === null) {
            val = 'null';
        } else {
            type = typeof inputArg;
            if (type === 'string') {
                val = inputArg;
            } else if (type === 'undefined') {
                val = type;
            } else {
                if (type === 'symbol') {
                    throw new TypeError('Cannot convert symbol to string');
                }

                val = String(inputArg);
            }
        }

        return val;
    }

    /**
     * Returns a string only if the arguments is coercible otherwise throws an
     * error.
     *
     * @private
     * @function
     * @param {*} inputArg
     * @throws {TypeError} If inputArg is null or undefined.
     * @return {string}
     */
    function $onlyCoercibleToString(inputArg) {
        return $toString($requireObjectCoercible(inputArg));
    }

    /**
     * The function evaluates the passed value and converts it to an integer.
     *
     * @private
     * @function
     * @param {*} inputArg The object to be converted to an integer.
     * @return {number} If the target value is NaN, null or undefined, 0 is
     *                   returned. If the target value is false, 0 is returned
     *                   and if true, 1 is returned.
     * @see http://www.ecma-international.org/ecma-262/5.1/#sec-9.4
     */
    function $toInteger(inputArg) {
        var number = +inputArg,
            val = 0;

        if ($strictEqual(number, number)) {
            if (!number || number === Infinity || number === -Infinity) {
                val = number;
            } else {
                val = (number > 0 || -1) * Math.floor(Math.abs(number));
            }
        }

        return val;
    }

    /**
     * The abstract operation ToLength converts its argument to an integer
     * suitable for use as the length of an array-like object.
     *
     * @private
     * @function
     * @param {*} inputArg The object to be converted to a length.
     * @return {number} If len <= +0 then +0 else if len is +INFINITY then
     *                   2^53-1 else min(len, 2^53-1).
     * @see https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
     */
    function $toLength(inputArg) {
        return Math.min(Math.max($toInteger(inputArg), 0), MAX_SAFE_INTEGER);
    }

    if (!String.prototype.toChunks) {
        /**
         * This method chunks a string into an array of strings of a specified
         * chunk size.
         *
         * @function
         * @this {string} The string to be chunked.
         * @param {Number} chunkSize The size of the chunks that the string will
         *                           be chunked into.
         * @returns {Array} Returns an array of the chunked string.
         */
        $defineProperty(String.prototype, 'toChunks', {
            enumerable: false,
            configurable: true,
            writable: true,
            value: function (chunkSize) {
                var str = $onlyCoercibleToString(this),
                    chunkLength = $toInteger(chunkSize),
                    chunked = [],
                    numChunks,
                    length,
                    index,
                    start,
                    end;

                if (chunkLength < 1) {
                    return chunked;
                }

                length = $toLength(str.length);
                numChunks = Math.ceil(length / chunkLength);
                index = 0;
                start = 0;
                end = chunkLength;
                chunked.length = numChunks;
                while (index < numChunks) {
                    chunked[index] = str.slice(start, end);
                    start = end;
                    end += chunkLength;
                    index += 1;
                }

                return chunked;
            }
        });
    }
}());

/*
 * Some tests
 */

(function () {
    'use strict';

    var pre = document.getElementById('out'),
        chunkSizes = [],
        maxChunkSize = 512,
        testString = '',
        maxTestString = 100000,
        chunkSize = 0,
        index = 1;

    while (chunkSize < maxChunkSize) {
        chunkSize = Math.pow(2, index);
        chunkSizes.push(chunkSize);
        index += 1;
    }

    index = 0;
    while (index < maxTestString) {
        testString += String.fromCharCode(Math.floor(Math.random() * 95) + 32);
        index += 1;
    }

    function log(result) {
        pre.appendChild(document.createTextNode(result + '\n'));
    }

    function test() {
        var strLength = testString.length,
            czLength = chunkSizes.length,
            czIndex = 0,
            czValue,
            result,
            numChunks,
            pass;

        while (czIndex < czLength) {
            czValue = chunkSizes[czIndex];
            numChunks = Math.ceil(strLength / czValue);
            result = testString.toChunks(czValue);
            czIndex += 1;
            log('chunksize: ' + czValue);
            log(' Number of chunks:');
            log('  Calculated: ' + numChunks);
            log('  Actual:' + result.length);
            pass = result.length === numChunks;
            log(' First chunk size: ' + result[0].length);
            pass = pass && result[0].length === czValue;
            log(' Passed: ' + pass);
            log('');
        }
    }

    test();
    log('');
    log('Simple test result');
    log('abcdefghijklmnopqrstuvwxyz'.toChunks(3));
}());
<pre id="out"></pre>


0

Utilizzando il metodo slice ():

function returnChunksArray(str, chunkSize) {
  var arr = [];
  while(str !== '') {
    arr.push(str.slice(0, chunkSize));
    str = str.slice(chunkSize);
  }
  return arr;
}

Lo stesso può essere fatto usando il metodo substring ().

function returnChunksArray(str, chunkSize) {
  var arr = [];
  while(str !== '') {
    arr.push(str.substring(0, chunkSize));
    str = str.substring(chunkSize);
  }
  return arr;
}

0

Il mio problema con la soluzione di cui sopra è che porta la stringa in blocchi di dimensioni formali indipendentemente dalla posizione nelle frasi.

Penso che il seguente approccio migliore; anche se ha bisogno di alcune modifiche alle prestazioni:

 static chunkString(str, length, size,delimiter='\n' ) {
        const result = [];
        for (let i = 0; i < str.length; i++) {
            const lastIndex = _.lastIndexOf(str, delimiter,size + i);
            result.push(str.substr(i, lastIndex - i));
            i = lastIndex;
        }
        return result;
    }

0

Puoi sicuramente fare qualcosa del genere

let pieces = "1234567890 ".split(/(.{2})/).filter(x => x.length == 2);

per ottenere questo:

[ '12', '34', '56', '78', '90' ]

Se si desidera inserire / regolare dinamicamente la dimensione del blocco in modo che i blocchi abbiano la dimensione n, è possibile effettuare ciò:

n = 2;
let pieces = "1234567890 ".split(new RegExp("(.{"+n.toString()+"})")).filter(x => x.length == n);

Per trovare tutti i blocchi di dimensioni n possibili nella stringa originale, prova questo:

let subs = new Set();
let n = 2;
let str = "1234567890 ";
let regex = new RegExp("(.{"+n.toString()+"})");     //set up regex expression dynamically encoded with n

for (let i = 0; i < n; i++){               //starting from all possible offsets from position 0 in the string
    let pieces = str.split(regex).filter(x => x.length == n);    //divide the string into chunks of size n...
    for (let p of pieces)                 //...and add the chunks to the set
        subs.add(p);
    str = str.substr(1);    //shift the string reading frame
}

Dovresti finire con:

[ '12', '23', '34', '45', '56', '67', '78', '89', '90', '0 ' ]

-1
    window.format = function(b, a) {
        if (!b || isNaN(+a)) return a;
        var a = b.charAt(0) == "-" ? -a : +a,
            j = a < 0 ? a = -a : 0,
            e = b.match(/[^\d\-\+#]/g),
            h = e && e[e.length - 1] || ".",
            e = e && e[1] && e[0] || ",",
            b = b.split(h),
            a = a.toFixed(b[1] && b[1].length),
            a = +a + "",
            d = b[1] && b[1].lastIndexOf("0"),
            c = a.split(".");
        if (!c[1] || c[1] && c[1].length <= d) a = (+a).toFixed(d + 1);
        d = b[0].split(e);
        b[0] = d.join("");
        var f = b[0] && b[0].indexOf("0");
        if (f > -1)
            for (; c[0].length < b[0].length - f;) c[0] = "0" + c[0];
        else +c[0] == 0 && (c[0] = "");
        a = a.split(".");
        a[0] = c[0];
        if (c = d[1] && d[d.length -
                1].length) {
            for (var d = a[0], f = "", k = d.length % c, g = 0, i = d.length; g < i; g++) f += d.charAt(g), !((g - k + 1) % c) && g < i - c && (f += e);
            a[0] = f
        }
        a[1] = b[1] && a[1] ? h + a[1] : "";
        return (j ? "-" : "") + a[0] + a[1]
    };

var str="1234567890";
var formatstr=format( "##,###.", str);
alert(formatstr);


This will split the string in reverse order with comma separated after 3 char's. If you want you can change the position.

-1

Che dire di questo piccolo pezzo di codice:

function splitME(str, size) {
    let subStr = new RegExp('.{1,' + size + '}', 'g');
    return str.match(subStr);
};

-2
function chunkString(str, length = 10) {
    let result = [],
        offset = 0;
    if (str.length <= length) return result.push(str) && result;
    while (offset < str.length) {
        result.push(str.substr(offset, length));
        offset += length;
    }
    return result;
}

4
La tua risposta non aggiunge nulla di nuovo (rispetto alle altre risposte) e manca di una descrizione come le altre risposte.
flob
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.