Breaking Up a String into Equal Parts

Presented with a string of concatenated ID's, I had to break them up into a comma delimited list. These ID's were all stored in a database field concatenated in one long string with no breaks, tabs or other delimiters. Why, I don't know.

Fortunately, I knew that all the ID's were 6 characters each. I just had to come up with the regular expression to separate them and insert a comma and a space.

Here's the ColdFusion code to do that:

view plain print about
1<cfset MyLongStringOfConcatenatedIDs = "1234567890qwertyuiopasdfghjklz" />
2
3<cfset CommaDelimList =
4REReplace(Trim(MyLongStringOfConcatenatedIDs), "(.{6})\B", "\1, ", "ALL") /
>

5
6output: "123456, 7890qw, ertyui, opasdf, ghjklz"

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
UPDATE:

What if you want to pick a specific ID out of the list?
Here's how!

cfset MySpecificID =ListGetAt(MyLongStringOfConcatenatedIDs, (ListLen(MyLongStringOfConcatenatedIDs) - 1))

The example above pulls out the 2nd to last ID or list item.
# Posted By CF Joe | 4/25/18 5:05 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.7. Contact Blog Owner