how to break a string into substrings with specified delimiters?

442 views
Skip to first unread message

J Y

unread,
Aug 13, 2023, 2:09:21 AM8/13/23
to Google Apps Script Community
Hi,

I have a need to break a string into substrings based on a set of delimiters.  For example, I have a string, "ab @ cd % ef * ghi".  The delimiters are @ % and *.  Then I would need to break the string into substrings, "ab", "cd", "ef", and "ghi".  

Can anyone kindly help me out?  I really appreciate it.

JD

Tanaike

unread,
Aug 13, 2023, 3:49:12 AM8/13/23
to Google Apps Script Community
Unfortunately, from your question, I couldn't understand whether you want a Google Apps Script or a formula in a Spreadsheet. So, how about the following patterns?

Google Apps Script.

console.log("ab @ cd % ef * ghi".split(/[^a-zA-Z]+/g)) // <--- [ 'ab', 'cd', 'ef', 'ghi' ]

Formula of the Spreadsheet

=SPLIT(REGEXREPLACE("ab @ cd % ef * ghi","[^a-zA-Z]+","@"),"@")


Jiandong

unread,
Aug 13, 2023, 11:57:52 AM8/13/23
to google-apps-sc...@googlegroups.com
Hi Tanaike, 

I am sorry for the confusion. I am talking about google app script. Thank you for your formula though. I appreciate it if you can help me out further.

JD

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/1tSt7PV4HZo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/bfa2f648-bf14-4773-90b9-6885b41e82bbn%40googlegroups.com.

Jiandong

unread,
Aug 13, 2023, 5:49:20 PM8/13/23
to google-apps-sc...@googlegroups.com
Hi Tanaike,

I am so sorry that I ignored your google apps script when dropping you a quick email from my phone. Now I am so thankful that you provided two solutions. Regarding the google apps script you provided, it’s close to my need. But it won’t work if I include any characters other than alphabetical characters. Can you kindly show me how to modify the statement from including alphabets to excluding the specified delimiters?  I am really sorry that I did not make it clear at the first place.

JD
Reply all
Reply to author
Forward
0 new messages