0

I'm new in Javascript. I have the string and the array of some indices.

var string = "?str!ing."; //input string
var arr = [0, 4, 8]; // indices of the punctuation signs in the string

How to find the substrings:

["?", "str", "!", "ing", "."]

Thanks in advance.

dlx
  • 59
  • 7
  • 1
    possible duplicate of [Split a string, at every nth position, with JavaScript?](http://stackoverflow.com/questions/12686746/split-a-string-at-every-nth-position-with-javascript) – Jamie Barker Jul 15 '15 at 08:36

3 Answers3

1

If you already have the indices inside arr, this will fill a variable res with the substrings:

var res = [];
if(arr[0] > 0) res.push(string.substring(0, arr[0])); //only needed if the first index is not the start of the string and the first substring has to be added
for(var i = 0; i< arr.length; i++){
    var index = arr[i]; 
    res.push(string.substring(index,++index)); //add the puncutation sign 
    var next = arr[i+1] || string.length; //get the next index (or end of string)
    if(index<next) res.push(string.substring(index, next)); //add the substring if length > 0
}

fiddle

Perhaps another way would be to build the res array at the same time as determining the indices, but the above should work if you already have the indices array.

Me.Name
  • 12,259
  • 3
  • 31
  • 48
  • E.g. if the input string: string="?!"; arr=[0,1]; then res = [?, '', !] I add one more loop to remove emptu element: http://jsfiddle.net/ckry4m15/ – dlx Jul 15 '15 at 09:34
  • aha, that's a possibility too, hadn't taken that into account. Altered the 'end of string' check to a substring length greater than 0 check to achieve the same effect. That way no extra loop is needed. (http://jsfiddle.net/dtnyt204/2/ ) – Me.Name Jul 15 '15 at 09:45
0

I believe this code will break the sentence according to the indices.

var length = arr.length;
var responce = [];

for (var x = 0; x<length ; x++){
    if(arr[x] == 0){
        var z = string.slice(arr[x],arr[x]+1);
        responce.push(z)
    }else{
        var z = string.slice(arr[x-1]+1,arr[x]);
        responce.push(z)
        var z = string.slice(arr[x],arr[x]+1);
        responce.push(z)
    }
    if((x == length-1) && (arr[x] < string.length-1)){
        var z = string.slice(arr[x]+1,string.length);
        responce.push(z)
    }
}

console.log(responce);
cs04iz1
  • 1,737
  • 1
  • 17
  • 30
-1

Fast solution is to have a pre-defined list of your separators:

splitString = function(string, splitters) {
    var list = [string];
    for(var i=0, len=splitters.length; i<len; i++) {
        traverseList(list, splitters[i], 0);
    }
    return flatten(list);
}

traverseList = function(list, splitter, index) {
    if(list[index]) {
        if((list.constructor !== String) && (list[index].constructor === String))
            (list[index] != list[index].split(splitter)) ? list[index] = list[index].split(splitter) : null;
        (list[index].constructor === Array) ? traverseList(list[index], splitter, 0) : null;
        (list.constructor === Array) ? traverseList(list, splitter, index+1) : null;    
    }
}

flatten = function(arr) {
    return arr.reduce(function(acc, val) {
        return acc.concat(val.constructor === Array ? flatten(val) : val);
    },[]);
}

var stringToSplit = "?str!ing.";
var splitList = ["?", "!", "."];
splitString(stringToSplit, splitList);

Example above returns: ["str", "ing"]

Mouneer
  • 12,827
  • 2
  • 35
  • 45