Jul 16 2008

Removing spaces from a string with coldfusion

Posted by mark at 7:50 PM
6 comments
- Categories: Coldfusion

Ran into this problem today and it seemed to take quite a long time for me to find a solution so I'm posting how it works here for the benefit of others. This method uses a regular expression to do it's bidding.

<!--- set up a test variable --->
<cfset mySpaceFilledString = "a brand new combine harvester" />

<!--- remove ALL spaces from the string --->
<cfset mySpaceFreeVariable = ReReplace(mySpaceFilledString, "[[:space:]]","","ALL")>

<!--- check it works --->
<cfdump var = #mySpaceFreeVariable#>

this returns "abrandnewcombineharvester" - all spaces removed from the whole string



To simply remove the spaces at the beginning and end of a string (a form field for example) it's much easier...

<!--- set up a test variable --->
<cfset form.myTestString = " mark mcaulay " />

<!--- remove spaces from start and end of the string --->
<cfset mySpaceFreeVariable = Trim(form.myTestString)>

<!--- check it works --->

<cfdump var = #mySpaceFreeVariable#>


this returns "mark mcaulay" - only the spaces at the start and end of the string have been removed

Hope this helps someone!

**UPDATE - Just for good measure, and of course, to keep James happy ;) here is the equivelent in php

<?php

// set up a test variable

$mySpaceFilledString = " a brand new combine harvester ";

// remove all spaces from the string

$mySpaceFreeVariable = str_replace(" ", "", $mySpaceFilledString);

// check it works

echo $mySpaceFreeVariable;

?>

this returns "abrandnewcombineharvester" - all spaces removed from the whole string

To simply remove the spaces at the beginning and end of a string (a form field for example) it's again, much easier...

<?php

// set up a test variable

$myFormVariable = " mark mcaulay ";

// remove spaces from start and end of the string

$myTrimmedVariable = trim($mySpaceFilledString);

//check it works

echo $myTrimmedVariable;

?>

this returns "mark mcaulay" - only the spaces at the start and end of the string have been removed

Comments

James Buckingham

James Buckingham wrote on 07/18/08 9:47 AM

What would be really good to see from you Mark is a comparison of your experiences with PHP and ColdFusion.

Even something as simple as building and outputting a query hopefully will demostrate how easy it is to learn/use CF.

In fact bugger it. It's so easy I'll write it in here :-).

1) Create a query

<cfquery name="getUsers" datasource="myDatabase">
select name,email,telno
from user
</cfquery>

2) Output into a list

<ul>
<cfoutput query="getUsers">
<li>#name#</li>
<li>#email#</li>
<li>#telno#</li>
</cfoutput>
</ul>

-----
Now correct me if I'm wrong but can PHP/ASP do that as easily? :-)
mark

mark wrote on 07/18/08 11:06 AM

challenge received James...it's actually fairly straightforward to replicate your query output with php...here it is:
1) set up your connection to the DB , this is what the coldfusion datasource is taking care of...but someone still needs to set the datasource up ;)
<?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("myDatabase") or die(mysql_error());

2) retrieve the required data from the user table (just like your <cfquery> is doing.

$getUsers = mysql_query("SELECT name, email, telno FROM user")
or die(mysql_error());

3) Output into a list

echo "<ul>";
while($row = mysql_fetch_array( $getUsers ))
{
   echo "<li>".$row['name']."</li>";
   echo "<li>".$row['email']."</li>";
   echo "<li>".$row['telno']."</li>";
}
echo "</ul>";
?>
James Buckingham

James Buckingham wrote on 07/18/08 11:21 AM

Cool nice one. I think I should have rephrased that last question actually.

I don't want to get into a "your language is better than my language" debate as we'll just go round in circles but the main point I was trying to make is the readability of CF

I don't know about you but I find:
<li>#name#</li>

a hell of a lot easier to write and read than:
echo "<li>".$row['name']."</li>";

Double check - did I get all the double/single quotes right in that last line. Yeap all 6 of them are in there :-)

And that's a very basic example of the simplicity that CF brings.

It's worth a serious look at the language if you've not yet, and with the announcement that Railo and BlueDragon are now open source there isn't that price tag attached to it anymore.

You know me as well, if anybody shows the slightest interest I'm more than happy to help and support ;-)

James
mark

mark wrote on 07/18/08 11:34 AM

you dont need to preach to me mate, i'm loving coldfusion! I wholly recommend other developers give it their attention, some things just seem easier and i'll no doubt be blogging those things as time goes on.

Just for good measure though, i've updated the post to include the php method of doing the same string replacement. I intend to spend a lot more time with coldfusion and you're right, with openBD and Railo coming later in the year, there is less need for companies to fork out a license fee for "proper" coldfusion these days either if they dont want to.
James Buckingham

James Buckingham wrote on 07/18/08 11:42 AM

Lol, thanks. I think :-)

Of course the license for the Adobe CF engine does pay for itself very quickly when you think of the man-hours you save in development and maintenance.

I don't know if you've found this as you're learning but my experience with PHP has always made me say "I can do this easier in CF by doing X,Y,Z".

I've not even mentioned the out the box Admin system and server monitoring tools they give you. Oh hang on, do I work for Adobe sales or something, lol??

Cheers,
James
duncan

duncan wrote on 10/17/08 4:22 PM

Your code to strip spaces is different between CF and PHP. The CF version you've used a regex, but the PHP version isn't. In fact, using your example string, you don't need the regex in CF at all. Just use

<cfset mySpaceFreeVariable = Replace(mySpaceFilledString, " ","","ALL")>

(or ReplaceNoCase).

Which would then be the equivalent of the str_replace version. The [:space:] posix class will match newlines, tabs etc.

Write your comment



(it will not be displayed)