Split JavaScript var?

I have var: "var board = 41–75—–53–7–2-36-81–7-9–25-1-3–9-47–2-1-7—6587–9—–26-8–1925—47" and I want it to be split so that it looks like this:

var boardSplit = [

"41–75—",
"–53–7–",
"2-36-81–",
"7-9–25-1",
"-3–9-47-",
"-2-1-7—",
"6587–9–",
"—26-8–",
"1925—47"
]

So the 81 characters are automatically divided into nine, nine long blocks of numbers.

I hope this is possible so someone can help me.

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
15 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
bdhejaksb
8 months ago

You want to split a long string in JavaScript into an array, each element being exactly 9 characters long. This is easy with a loop. Here’s the code:

var board = "41--75-----53--7--2-36-81--7-9--25-1-3--9-47--2-1-7---6587--9-----26-8--1925---47";
var boardAufgeteilt = [];

for (var i = 0; i < board.length; i += 9) {
    boardAufgeteilt.push(board.substring(i, i + 9));
}

console.log(boardAufgeteilt);

How it works:

  1. We have a long string board.
  2. With a loop we pass through this string in steps of 9 characters.
  3. In each step we take 9 characters and add them to the array board.
  4. At the end we have an array in which each element is a 9-character long part of the original string.

The result looks like this:

[
  "41--75---",
  "--53--7--",
  "2-36-81--",
  "7-9--25-1",
  "-3--9-47-",
  "-2-1-7---",
  "6587--9--",
  "---26-8--",
  "1925---47"
]


GuteAntwort2021
8 months ago

Hello.

Never done anything with Javascript, so syntax could be wrong, but so ca.

For simple output:

for (let i = 0; i < board.length; i += 9) {
    console.log(board.substring(i, i + 9));
}

If they are to be stored instead as individual blocks:

let substrings = [];
for (let i = 0; i < board.length; i += 9) {
    substrings.push(board.substring(i, i + 9));
}

This is now statically divided into 9 characters per block.

LG

MrAmazing2
8 months ago

const substrings*

You don’t change it

GuteAntwort2021
8 months ago
Reply to  MrAmazing2

I know what he’s doing with the blocks. His information content on the question was very poor. 😉

Erzesel
8 months ago

..and why explicitly calculate the end of each Chunk?

substr() requires only the starting position and the length of the desired partial string

let board = "41--75-----53--7--2-36-81--7-9--25-1-3--9-47--2-1-7---6587--9-----26-8--1925---47" ;
const chunkSize = 9
let chunks=[]
for (let i = 0; i < board.length; i += chunkSize) {
    chunks.push(board.substr(i,chunkSize));
}
console.log(board)
console.log(chunks)
GuteAntwort2021
8 months ago
Reply to  Erzesel

Overalls you can of course use the type start + length, instead of start + end. But the performance is unlikely to be measurable.

I even suspect that it uses the same algorithm to calculate the endpoint only in the function. So jacket like pants. 😉

Erzesel
8 months ago

Thank you. I had not been on the screen

MrAmazing2
8 months ago
const boardAufgeteilt = board.match(/.{9}/g);
MrAmazing2
8 months ago
Reply to  MrAmazing2

(please not ask in today’s time var use more. That makes some problems. Instead you use let and const).

GuteAntwort2021
8 months ago
Reply to  MrAmazing2

Beautiful solution with regex! 👍 But for beginners probably not really to understand.

MrAmazing2
8 months ago

Thank you. 😀

Yes, I could add an explanation. If the questioner asks:P

Erzesel
8 months ago
Reply to  MrAmazing2

I would have preferred the RegEx solution purely intuitively. (Becauses elegant looks)

…aaaaber… for short strings, the performance is great.

 let board = "41--75-----53--7--2-36-81--7-9--25-1-3--9-47--2-1-7---6587--9-----26-8--1925---47" let chunks=[] console.time("perfSubstrLoop") for (let i = 0; i < board.length; i += 9) {  chunks.push(board.substr(i,9)); } console.timeEnd("perfSubstrLoop") console.time("perfRegexSplit") chunks = board.match(/.{9}/g) console.timeEnd("perfRegexSplit") //perfSubstrLoop: 0.02392578125 ms //perfRegexSplit: 0.302001953125 ms

In the RegEx method, you have the entire overhead for initializing the RegEx project (which does not see due to implicit use)

….it is only fractions of milliseconds, but depending on what one has to do and how many times the operation happens, one should consider what one uses.

Erzesel
8 months ago
Reply to  Erzesel

PS short strings are a very relative term…

let board = "41--75-----53--7--2-36-81--7-9--25-1-3--9-47--2-1-7---6587--9-----26-8--1925---47".repeat(1000000)
//...usw...

//perfSubstrLoop: 953.126220703125 ms
//perfRegexSplit: 1122.876953125 ms
cleanercode
8 months ago

The RegEx engine of JS seems to work clean. I am am amazed:)