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

How to replace all characters in string?

New Here ,
Jan 06, 2010 Jan 06, 2010

Hi

I'm new in cold fusion and i hawe to change some characters in string

For example i have path like that

file://///salem/libres/ACTIVE/

and i need to change it into:

\\salem\libraries\ACTIVE\

how can i do that there is no replaceAll function

how can i do that in the simplest way?

1.8K
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 ,
Jan 06, 2010 Jan 06, 2010

Well there's no replaceAll() function, no.  But did you read the docs for replace() closely?

--

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
Valorous Hero ,
Jan 06, 2010 Jan 06, 2010

Ah man, Adam beat me to the reply this morning.

Read the documentation on all the replace functions.

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
Jan 07, 2010 Jan 07, 2010

<cfset abc = "/////salem/libres/ACTIVE/">

<cfset def = replace(abc,"/","\","ALL")>

<cfoutput>#def#</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
Community Expert ,
Jan 10, 2010 Jan 10, 2010

@T.Brudnicki

how can i do that there is no replaceAll function

Yes, there is. In fact, there are quite a number of replaceAll  functions in Coldfusion. Take Replace, ReplaceNoCase, REReplace, REReplaceNoCase and ReplaceList. Each of the first four has an attribute called scope, which can take the value "all". This makes the functions literally replace-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
Engaged ,
Jan 11, 2010 Jan 11, 2010

As a blanket rule-of-thumb, anytime you find yourself saying, "I want to find and/or replace things within a string," exactly one thought should immediately pop into your head:  "regular expressions."

Even though they are often accused of being "incomprehensible chicken-scratches," regular expressions are actually one of the most powerful concepts ever implemented.  (And they have been very well implemented.)

Why don't you see a bewildering library of "replaceAll" functions?  Because they've got Regular Expressions!  (So, "why bother?")

Go spend a good solid hour surfing the results of "regular expression tutorial" on Google.  The effort will be worth every second that you spend on it, both now and for years to come.  Trust me:  after maybe a little bit of such as you may expect while learning anything new,  "the little light will come on," very quickly, and you'll be saying:  wow!

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
Jan 11, 2010 Jan 11, 2010

In total agreement with TLC-IT

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
Valorous Hero ,
Jan 11, 2010 Jan 11, 2010
exactly one thought should immediately pop into your head:  "regular expressions."


Well, I would not say it should be the only thought 😉 For certain simple operations, it can be overkill.. (and yes, some regex's _are_ chicken scratch).  But I do agree that when the basic pattern is complex, or you have a variable number of X or Y characters, then you are better off considering a regular expression.

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 ,
Jan 14, 2010 Jan 14, 2010
LATEST

As a blanket rule-of-thumb, anytime you find yourself saying, "I want to find and/or replace things within a string," exactly one thought should immediately pop into your head:  "regular expressions."

Disagree.  If what is being matched (for a find or a replace) is a simple literal string, then using regex technology to deal with it is overkill.  The best tool for this situation is a vanilla replace().

--

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
Jan 11, 2010 Jan 11, 2010

All in all it depends on how many files you are talking about having to clean up if its a

handful just spend the time an hour whatever and just modify by hand. If it is a whole site and hundreds then I would invest in

learning about Regular Expressions and you can write yourself a little recursive application in coldfusion to traverse the folder structure

searching for the string in all files of type i.e .cfm and replace string in it.

This is something I had done about 8 years ago when converting the BigTen website from static html to ColdFusion. It was a whole lotta fun learning about it.

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 ,
Jan 13, 2010 Jan 13, 2010

I'll stick to my original statement.  "No, it's not 'overkill,' and when you get a little bit into Regular Expressions you'll see first-hand what I mean by that."

Regexes are a very thorough and very general-purpose solution to one of the otherwise-most-vexing problems that we all face:  "dealing with character strings."  In practice it is very cumbersome to write functions to do this sort of thing ... in any language at all, yes, but in the case of CF, "most especially if you have not yet delved deeply into the world of <cfscript>."

CF's implementation is basically, as far as I can see, "Perl compatible."  (We do know that their implementation is undoubtedly going to be, of course, "Java's," because Java is the underlying architecture now.)  So there are a lot of resources here, that CF has simply tapped into.  You are therefore dealing with an extremely robust technology, of known high quality, and you're using a modus operandi for string manipulation that spans far beyond the world of Cold Fusion.

So...  dive in, at once.  Don't linger on the edge of the pool.  It will change the way that you think about string manipulation, and the learning curve (while "peculiar" at first) is certainly not insurmountable.  There are plenty of "regular expression testers" and "try it yourself demos" out there, all of which apply directly to the Regular Expression implementation that is surfaced in Cold Fusion.

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
Valorous Hero ,
Jan 13, 2010 Jan 13, 2010

I would agree they are typically under-utilized. But I would not say that regexes are the right for every job. For simple replaces of a single character or two, I still use the plain replace() functions every time. So we will have to agree to differ here

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