how to find white space between the string in coldfusion - coldfusion

I need to find the white space between a string in ColdFusion
for example
str="ha ppy"
Whitespace=4
i need to calculate how many white space between the str.
how to do that.

A simple regex:
<cfset str="ha ppy">
<cfset spaces = reReplace(str, "\S+(\s+)\S+", "\1")>
<cfoutput>
<pre>spaces = [#spaces#]</pre><br>
#len(spaces)#
</cfoutput>

Related

Finding just one or more comma in a string with ColdFusion

How can I detect if a string value only consists of one or more commas and nothing else. The correct value should be something like: ABC,BVC,BNM but sometimes I get value like: , or ,,, or ,, and this is not allowed. How can I detect that a string only have one or more commas and then I can create a warning to the user and stop the process.
Thank you
You can use listToArray() and arrayToList() to remove the empty items from the list and can then compare the sanitized version with the original like this:
<cfset originalInput = trim( ",,," )>
<cfset sanitizedInput = arrayToList( listToArray( originalInput, ",", false ), "," )>
<!--- Compare both --->
<cfif originalInput NEQ sanitizedInput>
<!--- Throw error --->
</cfif>
Depends on how much your input may vary, but as you currently describe it:
Something as simple as <cfif MyVar contains ",,"> would work.
If one comma (and nothing else) is a possibility, then
<cfif MyVar contains ",," OR Len(MyVar) lt 2>
Assuming any non-commas are either letters or numbers, you can do use a regular expression:
patternAlphaNumeric = "[0-9a-zA-Z]";
testString = ",,,";
if (reFind(",", testString) > 0 && refind(patternAlphaNumeric, testString) == 0)
code for all commas
else
code for other characters
If you are only concerned about detecting commas (or one or more of any character), just use ListLen().
It's a native ColdFusion function.
It ignores empty list items by default.
Its default delimiter is a comma.
So, if your_string consists only of one or more commas, then ListLen( your_string ) will always return 0.
Heads up, it also returns 0 for an empty string, so if you don't want your code to pop for empty strings, be sure to account for that.
<cfset local.myString = "string-goes-here">
<cfset local.myNewString = ReReplace(trim(local.myString),",","","ALL")>
<cfif not len(local.myNewString)>
<!--- warning to the user and stop the process --->
</cfif>

How to remove words or chars from a string using coldfusion

I am new to coldfusion and my goal is to remove part of a string according to certain words.
For example:
<cfset myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure"/>ยจ
How can I remove the words "One of the myths associated with the" in order to
have
Great Wall of China is that it is the only man-made structure as string?
I used following function
RemoveChars(string, start, count)
But I need to create a function maybe with RegEx or native coldfusion functions.
I see this question already has an accepted answer, but I thought I'd add another answer :)
You can do it by finding where the word 'Great' is in the string. With modern CFML you can do it like so:
<cfscript>
myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure";
// where is the word 'Great'?
a = myVar.FindNoCase("Great");
substring = myVar.removeChars(1, a-1);
writeDump(substring);
</cfscript>
Using mid would give you a bit more flexibility if you want to cut chars off both ends.
<cfscript>
myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure";
// where is the word 'Great'?
a = myVar.FindNoCase("Great");
// get the substring
substring = myVar.mid(a, myVar.len());
writeDump(substring);
</cfscript>
In older versions of CF that would be written as:
<cfscript>
myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure";
// where is the word 'Great'
a = FindNoCase("Great", myVar);
// get the substring
substring = mid(myVar, a, len(myVar));
writeDump(substring);
</cfscript>
You could also use a Regular Expression to achieve the same result, you'll have to decide which is more appropriate in your use case:
<cfscript>
myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure";
// strip all chars before 'Great'
substring = myVar.reReplaceNoCase(".+(Great)", "\1");
writeDump(substring);
</cfscript>
You could see the sentence as a list seperated by spaces. So if you want to cut off your sentence to start with "Great Wall of China", you could try
<cfloop list="#myVar#" index="word" delimiters=" ">
<cfif word neq "Great">
<cfset myVar = listRest(#myVar#," ")>
<cfelse>
<cfbreak>
</cfif>
</cfloop>
<cfoutput>#myVar#</cfoutput>
There may be a quicker way to do this. Here's a function at cfLib.org that can alter a list in a similar way: LINK.

Getting string between two characters - Coldfusion

I'm struggling a bit with ColdFusion (not the language I ever write in).
I am trying to do a regex to get a part of a string.
So for example, if my string is: D_CECILA23_CEC23423
I want the part that is between the 2 underscores.
This is the code I have so far, and it works for anything that is alpha characters, but when a number is thrown into the mix, it just breaks.
<cfset myStr = "D_CELCI_LISA">
<cfset myStr2 = reReplace(myStr, "([\w\d\%]+)(\_)([/ A-Z]+)(\_)([\w\d\?]+)", "\3", "all") >
<cfoutput>
myStr: #myStr#<br />
myStr2: #myStr2#<br />
</cfoutput>
Which gives me:
myStr: D_CELCI_LISA
myStr2: CELCI
If it really is as simple as getting the text between the first and second underscore character, you don't need a regex. This'll do it:
myStr2 = listGetAt(myStr, 2, "_");
That said, this should do for the regex in that context: ^.*_([^_]+)_.*$, eg:
myStr2 = reReplace(myStr, "^.*_([^_]+)_.*$", "\1", "all");
#user2429578 ListLast() and ListFirst() for the last or first element of a list.

Regex to find length in description

<cfset RegexToFindLength = "Length:.*?(\d*\.?\d+)\s*(""|")"/>
<cfset Description = "blah blah blah 2.5"""/>
<cfset size = #reMatch(RegexToFindLength, Description)# />
<cfdump var="#size#">
Error Message: ColdFusion was looking at the following text:
)
looking to extract Length: 2.5" from the products description.
I have tested the above regex expression in regexpal and it works. But when i try using it in a cfm page, i get errors.
Can someone explain to me how this would be setup in CF?
You have a few issues here.
1) You don't escape your double quotes, so you end up closing your regex string and confusing it.
Personally, when I have to use double quotes in a string, I tend to use single quotes to define the string if I can.
<cfset RegexToFindLength = 'Length:.*?(\d*\.?\d+)\s*(""|")'/>
2) Your Description variable doesn't have the string you're searching for, so there will be no match. I changed this to the following to make it work (note the single quotes for defining the string):
<cfset Description = 'Length:.:2.5""'/>
3) (maybe not an issue) Size is not being set to a number. rematch returns an array of strings. You'll want to check the length of the string inside the array positions or check the length of the array itself - I don't know what exactly it is that you want to do.

How to return partial string in ColdFusion

Lets say I have a string The big brown fox jumped<br> over the fence. How could I specify that I want to keep only the characters that are before the <br> tag, stripping out the rest?
I use something similar, don't know if it's more efficient.
<cfset string = "The big brown fox jumped<br> over the fence.">
<cfset firstPiece = listGetAt(string, 1, "<br>")>
<cfoutput>#firstPiece#</cfoutput>
I like it because I determine the output prior to output rather than on output. But orangepips is nice because you could tailor your output based on the context..
EDIT
As pointed out in the comments the above code indeed produces "The" because of the way CF treats each character as a delimiter.
But <cfset firstPiece = listGetAt(string, 1, "<") produces "The big brown fox jumped".
From the CF Documentation
Delimiters
A string or a variable that contains
one. Character(s) that separate list
elements. The default value is comma.
If this parameter contains more than
one character, ColdFusion processes
each occurrence of each character as a
delimiter.
If you don't mind dropping down to a bit of the old java underlying CF... AColdFusion string is actually a java string. Java's split uses a regex, which at it's simplest can just be the string you want to split on. So unlike listToArray (which was extended in cf9 to allow multi-character splits, by the way), it is by definition multi-character. And since it's a regex, if you want it case insensitive, that too can be easily accomplished.
So given your string:
<cfset variables.myString = "The big brown fox jumped<br> over the fence." />
<cfset variables.myStringArray = variables.myString.split("(<[bB][Rr]>)",2) />
<cfset variables.myString = variables.myStringArray[1] />
variables.myStringArray will contain an array with at most 2 elements, the part before the first <br>, and the part after the first <br> (the second parameter to split, the 2, says to only split into 2 parts, at the most) occurrence, which will leave any <br>'s in the second part of your string intact.
Everyone loves regex 'cept me. So here's an approach using string functions instead:
<cfset str = "The big brown fox jumped<br> over the fence.">
<!--- roughly the equivalent of str.split("<br>") --->
<cfset pieces = listToArray(str, "<br>")>
<cfoutput>#pieces[1]#</cfoutput>
Update
Meh, don't have a CF server handy. Stupid listToArray(). Here's one that should work.
<cfset str = "The big brown fox jumped<br> over the fence.">
<cfset pos = find(str, "<br>")>
<!--- probably off by one here, don't have CF server handy --->
<cfset piece = left(str, pos)>
Update #2
Have access to a CF server now. This works. Addresses all the comments:
<cfset str = "The big brown fox jumped<br> over the fence.">
<cfset pos = findNoCase("<br>", str)>
<cfset piece = (pos ? left(str, pos - 1) : str)>
<cfoutput>#piece#</cfoutput>
Using Regexs:
<cfset str = "The big brown fox jumped<br> over the fence." />
<cfset firstPart = REReplaceNoCase( str, "<br>.*$", "") />
<cfoutput>#firstPart#</cfoutput>
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7a49.html
Why not use the listFirst function as in:
<cfset str = "The big brown fox jumped<br> over the fence." />
<cfset firstPart = listfirst(str, "<br>") />
<cfoutput>#firstPart#</cfoutput>