0

I am trying to split a string into an array with each iteration of my for-loop. Like if a string is 1234 then I want to split it ['12','34'].

I want to split this string into different ways. Like ['1','2','3','4'], ['123','4'], etc. But I don't know how can I do so?

The string "31173" can be split into prime numbers in 6 ways:

[3, 11, 7, 3]
[3, 11, 73]
[31, 17, 3]
[31, 173]
[311, 7, 3]
[311, 73]

let k=1;

for(let i=0; i<inputStr.length; i++){
        
if(k<inputStr.length){
    // split string 
    let splittedNums=inputStr.split('',i+k);
    for(let j=0; j<splittedNums.length; j++){
       if(isPrime(splittedNums[j]))
          result.push([splittedNums[j]]);
        
      }
   }
 k++;
}

I tried using the split() function but as I learned from the docs that it will use a limit to split the string and return it. So, It won't work like this.

I want to split and check whether the number is prime or not and then push it into the array. So that in the end, I will get the subarrays that contain prime numbers.

How can I split a string and then turn it into an array like this in javascript?

5
  • I wouldn't use split() as it's meant to take in a specific delimiter. I'd probably push() a substring() instead. Commented Jul 18, 2022 at 18:57
  • 2
    You want to split a string in seemingly completely random ways, the extent of which is not clear. Are you saying to want to split it into every possible combination of character groupings? Please be more clear on what you are trying to accomplish, otherwise no one will be able to help you. Commented Jul 18, 2022 at 18:57
  • @dqhendricks Yes, I want to split strings in ways and then check Is it a prime number or not. If it is prime then push it into an array. Commented Jul 18, 2022 at 19:06
  • @dqhendricks I have edited my question and one example. Please check Commented Jul 18, 2022 at 19:23
  • 2
    You can greatly reduce your search space by not splitting after digits that are multiples of 5 or 2. (whatever substring precedes them will not be prime) Commented Jul 18, 2022 at 19:26

1 Answer 1

1

One of the solution. I tried to explain the steps in the comment but just to summary

  1. Convert string to array
  2. Loop though the string array and split by index char each time.
  3. Recover the lost separator by concatenating the separator to last item of the array.

const str = "12345678";

// split the array
const strArr = str.split("");

// loop through the str array and split with a character got by index
const splitedStr = strArr.map((each, index) => {
    // this will split the string by index char but we will lose the separator passed
  const withoutSeparator = str.split(str[index]); 

  const lastIndex = withoutSeparator.length - 1; // get last element of array

  // to get the separator back.
  // basically we are appending the separator char to last item of the array 
  withoutSeparator[lastIndex] = str[index] + withoutSeparator[lastIndex];

  return withoutSeparator;
});

console.log({ splitedStr });

Output

{
  splitedStr: [
    [ '', '12345678' ],
    [ '1', '2345678' ],
    [ '12', '345678' ],
    [ '123', '45678' ],
    [ '1234', '5678' ],
    [ '12345', '678' ],
    [ '123456', '78' ],
    [ '1234567', '8' ]
  ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

it sounds like they want to split it into every combination of possible splits. so [12, 3, 456, 87], [123, 4, 56, 8, 7], etc. your code doesn't seem to meet requirement.
i misunderstood that part, with the comments it seems like he wants to split them by prime numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.