J
J
Johnny2016-10-11 19:38:41
JavaScript
Johnny, 2016-10-11 19:38:41

How to find the number and position of a character in a string (javascript)?

There are such strings (or an array of objects) of 96 characters:

000000000000000000000011111111111111100000000000000000000000000000111111111111111111111111111111
000000000000000000000000000000001111111111111111000000001111111111111111111111110000000000000000
000000000000000000000000000000001111111111111111111111111111110000000222222222222222200000000000

There can only be 3 characters: 0, 1, 2, I need to find the number and position of each group of characters, for example:
var str = '000000000000000000000000000000001111111111111111111111111111110000000222222222222222200000000000';

Что должно получиться:
zero: [
    {
        startPos: 0,
        count: 33
    }, {
        startPos: 63,
        count: 7
    }, {
        startPos: 86,
        count: 11
    }
],
one: [
    {
        startPos: 33,
        count: 30
    }
],
two: [
    {
        startPos: 70,
        count: 16
    }
]

Is it possible to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WQP, 2016-10-11
@GreysonKind

Break the string into an array, and then loop through this array in which you form a new one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question