1

I am trying to find a solution for a SQL problem that i think i can solve via JavaScript but cant conceptualize the solution and need some help with it. I have a long string (118.0000000000,102.6666666666,110.6666666666,97.5000000000,82.5000000000,86.6666666666 .... 15000 characters long) that i need to break into a max string length of 4000 (SQL NVARCHAR(max) limitation) and iterate the strings to a stored proc for further processing. Lets say using the string above i wanted to split the string at 35 character length but not lose data and terminate the new string at the last ',' before the 35th character and build the next string from that point on, so essentially strings would look like this:

118.0000000000,102.6666666666 
110.6666666666,97.5000000000
82.5000000000,86.6666666666

I tried to look through all the solutions already presented related to splliting string but have not been able to extract the info that could result in solving my issue here. Appreciate all the help!

3 Answers 3

1

If your strings are going to be really long, it might be faster to just work with indexes manually rather than breaking everything into arrays — it's not much less readable. For this you just look ahead to the comma beyond 35 characters with each loop, take the part up to the last comma and reset the position to where the comma was plus 1. If you don't have 35 characters left, just add the last bit and stop.

function splitGroups(str, MAX) {
    let res = []
    let start = 0
    
    while (start < str.length) {
        if (str.length - start <= 35) {
            res.push(str.slice(start))
            break
        }
        let c = str.slice(start, str.indexOf(',', start + MAX + 1))
        let end = c.lastIndexOf(',')
        res.push(c.slice(0, end))
        start += end + 1
    }
    return res    
}

let str = "118.0000000000,102.6666666666,110.6666666666,97.5000000000,82.5000000000,86.6666666666"

let groups  = splitGroups(str, 35)
console.log(groups)
console.log("lengths", groups.map(s => s.length) )

Sign up to request clarification or add additional context in comments.

3 Comments

another great solution! this is also applicable to my solution. Appreciate the help Mark!
working with large strings your solution was faster and hence i am marking this as the answer to my problem. Thanks again!
I upvoted. This is definitely a more performant solution than mine, I'll certainly admit that.
0

Use substr() to get the first 35 characters, split that into an array, then remove the last element of the array since it might not be a complete number.

var csv = "118.0000000000,102.6666666666,110.6666666666,97.5000000000,82.5000000000,86.6666666666";
var csv35 = substr(csv, 0, 35);
var nums = csv35.split(",");
nums.length--;

1 Comment

Thanks Barmar. The solutions below offered a more comprehensive solution to my problem thats why the 3rd answer got marked as the answer. Appreciate your time on this!
0

If I'm understanding your question correctly, you want to split a string at a separator in chunks of a predetermined maximum character length. Here's a functional approach that allows you to dynamically specify the length and separator:

const chunk = (maxLen, separator) => data => data.split(separator).reduce((chunks, v, i) => {
  const last = chunks.length - 1;

  if (chunks[last].length + separator.length + v.length <= maxLen) {
    chunks[last] += (i ? separator : '') + v;
  } else {
    chunks.push(v);
  }
  
  return chunks;
}, ['']);

let chunkAt35ByComma = chunk(35, ',')
let data = '118.0000000000,102.6666666666,110.6666666666,97.5000000000,82.5000000000,86.6666666666'
let chunks = chunkAt35ByComma(data)

console.log(chunks)

3 Comments

awesome! That's what i was looking for. Appreciate the help Patrick!
since the answer below yours resulted in better performance for large string i am marking that as the answer but your solution wasnt much behind as far as the performance goes. Thanks again!
@HarryGrewal glad you were able to obtain a working solution, that's what's important.

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.