Chunk a String in Xano (@nocodebackend)

Ray Deck shows how to split a string into two-character chunks and join them with commas in Xano. The function loops through the string length divided by two, extracts pairs like AB and CD, and handles odd-length strings by rounding up so leftover characters aren't dropped.
Chapters
So time to time I see interesting questions come up on the Xano community, the WeWeb community, or one of these others that is easier to show than to tell, and I whip up a video. This is one that I did last year about trying to break up a string into multiple even chunks. I think it can be a useful technique. I think a lot of people run into this before, and I hope you find it helpful. The goal, as I understand it, is you want to insert dashes in text so that your 1, 2, 3, 4, 5, or in this case A, B, C, D, E, F, G, would actually turn into like a, b, and then c, d, and then e, f, and then g would be left over at the end, right? So you're looking to do a split. So the way I would do that if I were wrapping this up into a function is I would have an input that would be a string just like you have here, and then I would set it up that we're going to split the string in pairs and chunk it up. And the way I would chunk it up is just through this kind of for loop that we have right here. And so I would create a variable called outputs that's just gonna be a blank array, and then I'm going to iterate over the string. Now in fact, what I'm going to do is iterate over the length of the string, right? And I'm going to iterate over the string length divided by 2, 'cause I want to group it into sets of 2. And then I'm going to round it. That way the 0.5 will actually round up. So if I have 7 elements, I want to actually get 4 groups, not 3. That way I make sure to actually catch the G at the end. And this rounding allows me to do that. And then I'll store whatever level we're at, 0, 1, 2, 3, in the variable called index. I'll then inside that loop say that I want to have a good— create a variable called pair, which is going to be the 2 elements in the string as of that index, right? So it's going to be as of first round, my index is 0 multiplied by 2, that's still 0. So for those 2 elements and length of 2, first 2, 'ab', are going to be my first pair. My second pair at index 1 would be 1 times 2 is 2. So at position 2, 3, because it's length of 3, 2, etc. And then that way, that's how I get my pairs. And I'm going to then take that pair and put it into outputs. And I'm doing this to make sure to set myself up for putting the commas in at the end. So now I've got my outputs, and that's fine. And I'm going to say, you know, we're going to take all those outputs right now. I've got my 4 pairs, which are my AB, my CD, my EF, and then G hanging out by itself at the end. And I'm going to put the commas in between. That way, I don't have an extra comma sitting out there at the end. I then tell the whole function to return output just by itself, and I'm going to close it up, and we give it a try. So I've got my A, B, C, D, E, F, G like we talked about at the beginning, and I run it, and in fact it gives me the exact split that I was looking for.










