string split returns array where some indexes are not available

17 views
Skip to first unread message

Dan Hinsley

unread,
May 6, 2026, 5:02:47 PM (2 days ago) May 6
to Google Apps Script Community
I have a string with delimiters that I use string.split to turn into an array.  Sometimes I get an array with the proper length (say 3) but when I try to access [1] or [2] they're not there.  If I'm in the debugger, I can see it says length 3, but when I expand it, there is only one item there.

For some additional context, I am using | (vertical bar) as my delimiter, which I now realize was maybe not the best choice as it's the pipe character.  I've changed from a simple split to using a regex and not sure if the problem will go away.  But before I do a lot more testing I thought I'd see if it was something obvious.

What I'm doing for the moment is using if (1 in fields) to check each index before I use it, but I hope there's a better way.

Keith Andersen

unread,
May 6, 2026, 5:12:06 PM (2 days ago) May 6
to google-apps-sc...@googlegroups.com
Spreadsheet to share?
Code to share?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/2f648438-44fc-4aaf-90df-eba04bc01c11n%40googlegroups.com.

Dan Hinsley

unread,
May 7, 2026, 11:41:59 AM (yesterday) May 7
to Google Apps Script Community
I pulled the code out into a test function with the new split with a regex, and it seems to be working.  Thanks.
Reply all
Reply to author
Forward
0 new messages