Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

insert comma at every third position in string...

Guest
Dec 07, 2010 Dec 07, 2010

I am flumoxed on something rather simple I fear.  I have a string like this ...

B01B09B20B21C13E10F07G12G20G24

I need to turn it into this

B01,B09,B20,B21,C13,E10,F07,G12,G20,G24

It is consistant in the following:

1. each segment will always be 3 characters long

2. each segment will always be structured as 1 character and 2 numerals

3. the list will always vary in length but always divisible by 3

Any simple solutions?  I have though about various cflooping methods and simply not liked anythign I cam up with.

All help is greatly appreciated.

God Bless!

Chris

1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Dec 07, 2010 Dec 07, 2010

Here are a couple options.  I prefer the first.

<cfset string = "B01B09B20B21C13E10F07G12G20G24" />

<cfset newstring = "" />

<cfloop from="1" to="#len(string)#" index="i" step="3">

<cfset newstring = listAppend(newstring, mid(string, i, 3), ",") />

</cfloop>

<cfoutput>#newstring#</cfoutput>

===========================================================================================

<cfset string = "B01B09B20B21C13E10F07G12G20G24" />

<cfset counter = 0 />

<!--- Iterate length - 4 times (-4 so that it does not do a final loop and stick a comma on the end) --->

<cfloop from="0" to="#len(string)-4#" index="i" step="3">

<cfset string = insert(",", string, i+counter+3) />

<cfset counter++ />

</cfloop>

<cfoutput>#string#</cfoutput>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 07, 2010 Dec 07, 2010

I don't think one needs to do all that looping about the place.

s1 = "B01B09B20B21C13E10F07G12G20G24";

s2 = reReplace(s1, "(.{3})\B", "\1,", "ALL");

That's all one needs to do.

As a rule of thumb, I find if one is confronted with a requirement to loop over a string to "do" something to it, then there's a good chance a regex operation will effect the same results.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Dec 08, 2010 Dec 08, 2010

One would agree that that is a more elagant solutions. But one did not have the regex knowledge to create it.

Regex is an awesome tool, but one would have to agree that it is not th most intuative.

In this case oneone does not mind looping. Of course onetwo is welcome ot disagree.

oneone

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 08, 2010 Dec 08, 2010

12Robots wrote:

One would agree that that is a more elagant solutions. But one did not have the regex knowledge to create it.

Regex is an awesome tool, but one would have to agree that it is not th most intuative.

In this case oneone does not mind looping. Of course onetwo is welcome ot disagree.

oneone

You do know that "one" is just a gender-non-specific third person singular pronoun, yes?  So - like - rather than "he" or "she" it just means "a(ny) person".  What it does not mean is "I" in that sense that wannabe-posh-but-illiterate people use it, that just makes them sound pretentious.  And thick.

I was using it in the former sense.  Being a NZer, I am about as far from "posh" as one can get.  I won't argue the toss as to how thick I am.  There's plenty of other people around prepared to do that.

And I believe Oneone was a race horse, according to tradition.  And Onetwo was a character in a Guy Richie film.  Not sure what they've got to do with anything, but I would be surprised if they knew much about either solution mooted here.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 08, 2010 Dec 08, 2010

You TWO made me laugh out loud.  Thanks for the great answers and even better entertainment.

I am thinking that maybe I am the cat in the hat and you should be named Thing one and Thing two.

Thank you both - for both

One    

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 08, 2010 Dec 08, 2010

RegEx is the epitome of power, with absolutely no usability in it whatsoever.

We have a saying, in that if you bash your keyboard, chances are you just created a legitimate RegEx.

It hints at how, for those trying to learn it, it can be a daunting language to comprehend and read.

Take looping for example:

for {x=1; x++; x<10} { x += 1; }

Can look somewhat complicated, because of syntax, but at least there are words in there "for, x, etc."

CF makes it easier by using tag-based syntax:

<cfloop from="1" to="10" index="i">

     <cfset x += 1 />

</cfloop>

This took the most text, but by far is the easiest for somone to pickup.  They can almost say the logic outloud.

With RegEx, individual characters stand for functions and characters, which is the epitome in optimized code, but it is extremely hard to comprehend.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 09, 2010 Dec 09, 2010

Another "One" agrees with this post. lol

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 09, 2010 Dec 09, 2010

Regexes - like anything else - are only complicated (~ looking) until one gives them a go.

And then - again, like anything else - one gets a handle on the basics... then the intermediary stuff... then one just feels comfortable.

However if one just looks at a regex and goes "I don't understand that", and makes no attempt to nut it out, then one is just gonna stay right there.

But taking the time to get up-to-speed with then is a good pay off as they're about the most powerful tool one has for string-manipulation, in an environment where our jobs are basically to generate strings...

--
Adam
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 09, 2010 Dec 09, 2010

I respectfully disagree Adam.  I know lots of programming languages/frameworks/patterns etc, but Regex is the one concept/syntax that I just can not grok no matter how many times I've tried to learn!!!111oneoneone

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 10, 2010 Dec 10, 2010
LATEST

I'd like to agree with Adam. I'd quite like to disrespectfully agree, but I'm not really sure how to do that.

For a long time (years?) I shyed away from Regex, opting to either find someone else to write them for me or by sticking to the warm and fuzzy feeling a simple loop would give me inside. Poor move.

It wasn't until this year when I had to write a bandwidth monitoring script in Perl that I really had no choice but to learn them properly, and I'm so very glad I did. The sheer power that they have is simply staggering, and what would take several lines of code without can be done in a few letters.

Take a text editor such as vi, which can perform regex search and replace. Say you have a text file and you simply want to trim whitespace from the start of each line? Notepad? Forget it. Vi?

:s/^\s//g

I've saved myself literally hours of work with little snippets like that.

I think the confusion often comes at the debugging stage; I do admit that trying to pick up and understand what someone else's regex is doing is very difficult, and often when writing my own I simply create it in a logical left to right pattern, and if it doesn't work I'll start again rather than trying to fix it.

One?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 08, 2010 Dec 08, 2010

I only wish I could have given you both the 'correct' answer as both were correct.

Thank you

Chris

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources