Split string into multiple parts based on string delimiter

I have the following string:

"<CSV>...</CSV>\n===END OF TABLE===\n### Statistical Analysis..."

I want to split this string into two parts:

Items = "<CSV>...</CSV>"
Analysis = "### Statistical Analysis..."

I tried to use the Split function but the delimiter can only be one character long. I could use regexMatches but I was wondering if there was any other way to do this. Or even if it is regexMatches, how would I go about doing this?

I would not bother with regex here, but simply use a combination of inStr and mid functions. In other words, look for the markers that begin and end the different parts (using the instr function) and then extract them using mid.

(If you want this to be more robust in case the data is malformed, you might first check that all the three markers exist within the string.)

let data = "<CSV>...</CSV>\n===END OF TABLE===\n### Statistical Analysis... nice data!"

let csvStartMarker = "<CSV>"
let csvEndMarker = "</CSV>"
let anslysisStartMarker = "### Statistical Analysis"

let csvPart = data.mid(data.inStr(csvStartMarker), data.inStr(csvEndMarker) + csvEndMarker.len());

let analysisPart = data.mid(data.inStr(anslysisStartMarker), data.len() - data.inStr(anslysisStartMarker));

return { csvPart, analysisPart }

I should have noted that the “### Statistical Analysis” part could change and that “===END OF TABLE===” is the only constant. Whatever comes after “===END OF TABLE===” could differ.

Same principle still applies: look for the index of the marker (“===END OF TABLE===”) in the text, and return the substring from that position + length of the marker.

1 Like