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

Eliminate Spaces from Character String

New Here ,
Oct 01, 2008 Oct 01, 2008
Hi,
Does anyone have a simple routine/function to eliminate all but 1 space between "words" (consectutive charactives with no spaces).

For example, I'd like to convert the string (I've used '.' as a "space":

"This...has....too..many...spaces" to
"This.has.too.many.spaces".

I need this to compare inputed character strings where a user may have hit the space bar an extra time or 2.

Thanks in advance.
824
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

correct answers 1 Correct answer

LEGEND , Oct 01, 2008 Oct 01, 2008
hefterr wrote:
> Thanks Dan. This looks good.
> Can I use ...
> <cfloop condition="mystring contains " ""> Not sure about the quotes for 2
> spaces

You need to alternate quotes when nesting them like that... Take your pick.

<cfloop condition="myString Contains ' '">
OR
<cfloop condition='myString Contains " "'>
Translate
Engaged ,
Oct 01, 2008 Oct 01, 2008
Look up the REREPLACE function. It sounds like what you want to do is replace all instances of a double space with a single space.

You also might consider using the TRIM function on your inputted fields to elimiate leading and trailing spaces.
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 ,
Oct 01, 2008 Oct 01, 2008
while(yourstring contains " ")
yourstring = replace(yourstring, " ", " ", "all");
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
New Here ,
Oct 01, 2008 Oct 01, 2008
Thanks Dan. This looks good.
Can I use ...
<cfloop condition="mystring contains " ""> Not sure about the quotes for 2 spaces
<cfset mystring = replace(mystring, " ", " ", all> replace 2 spaces with 1 space
</cfloop>

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 ,
Oct 01, 2008 Oct 01, 2008
hefterr wrote:
> Thanks Dan. This looks good.
> Can I use ...
> <cfloop condition="mystring contains " ""> Not sure about the quotes for 2
> spaces

You need to alternate quotes when nesting them like that... Take your pick.

<cfloop condition="myString Contains ' '">
OR
<cfloop condition='myString Contains " "'>
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
Engaged ,
Oct 01, 2008 Oct 01, 2008
Dear God please don't use a loop to solve this. Just use RegEx, reReplace(yourString, "\s+", " ", "ALL").

So much easier and cleaner!
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
New Here ,
Oct 01, 2008 Oct 01, 2008
Hi Stressed,
I tried both and actually went your way using :

<cfset s = rereplace(s, "[[:space:]]{2,}", " ","all")>
Is this the same regex as yours - I'm not well versed in regex's?
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
Engaged ,
Oct 01, 2008 Oct 01, 2008
LATEST
That is cool, your regex is essentially the same! Regex is brilliant, I hope you enjoy getting versed! 😄
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