I am trying to find a Delphi function to split an input string into an array or list of strings based on a delimiter. I have found many things on Google, but they all seem to have some problem or limitation and I have not found one that I am happy to work with.
I just have to split a string like :
'palabra:doc,txt,docx'
into an array based on ':'.
The result would be:
['palabra', 'doc,txt,docx']
.
Does anyone have a function that actually works?
Thank you
Original Q&A: https://stackoverflow.com/q/2625707/255257
For Delphi 7 the above does not work (and to also consider cases in which the separator is not a single character), to separate a string into two values I have the following procedure:
For what they ask, it would be called, for example, with:
I hope this helps someone...
You can use (in conjunction) the
DelimitedText
,StrictDelimiter
and propertiesDelimiter
of the classTStrings
and its descendants to split a string based on an arbitrary delimiter, as required.Basically the idea is to set the Delimiter, in this case
':'
setStrictDelimiter
toTrue
to prevent it from taking spaces and other non-printable characters as delimiters (if that's the case).Take a look at this example: