I have a string that I wish to remove some characters based on underscores in the string. For instance.
I wish to change
2_MASTER BEDROOM_CFM
to
MASTER BEDROOM
OR
2734923ie_BEDROOM 2_CFM
to
BEDROOM 2
Any recomendations on how to do this with coldfusion?
ColdFusion has the GetToken() function, which makes manipulating a string with a delimiter (virtually any delimiter) very easy. Assuming each string you're looking to parse is 2 sets of strings then this will output MASTER BEDROOM
<cfset String1 = '2_MASTER BEDROOM_CFM'>
<cfset FirstWord = ListFirst(String1,' ')>
<cfset FirstWord = GetToken(FirstWord,2,'_')>
<cfset SecondWord = ListLast(String1,' ')>
<cfset SecondWord = GetToken(SecondWord,1,'_')>
<cfoutput>
#FirstWord# #SecondWord#
</cfoutput>
Could also simplify it down to just
<cfset String1 = '2_MASTER BEDROOM_CFM'>
<cfoutput>
#GetToken(ListFirst(String1,' '),2,'_')# #GetToken(ListLast(String1,' '),1,'_')#
</cfoutput>
EDIT As Leigh points out in the comments you could also just use
getToken("2_MASTER BEDROOM_CFM", 2, "_")
This treats your string as a list with elements 2, MASTER BEDROOM, and CFM
So the string starts with some numbers and/or characters, then an underscore. Then some text, finally an underscore followed by CFM? Here's a regex that catches that:
^[a-z0-9]+_(.*)_CFM$
And here's some code that works for me:
<cfoutput>
<cfset String1 = '2_MASTER BEDROOM_CFM'>
<cfset yourString = reReplaceNoCase(String1, "^[a-z0-9]+_(.*)_CFM$", "\1")>
#yourString#<br>
<cfset String2 = "2734923ie_BEDROOM 2_CFM">
<cfset yourString = reReplaceNoCase(String2, "^[a-z0-9]+_(.*)_CFM$", "\1")>
#yourString#<br>
</cfoutput>
Related
I have a set of list values in ColdFusion variable, and I need to replace all the list values into desired text.
For Example:
<cfset headerColumnList = "FirstName,LastName,Email,FrequentGuestID,IP Address,Time Stamp Email Marketing">
<cfset a="test1">
<cfset b="test2">
<cfset c="test3">
<cfset d="test4">
<cfset e="test5">
<cfset f="test6">
<cfloop index = "ListElement" list= "#headerColumnList#" delimiters = ",">
<cfoutput>
#replaceList("#ListElement#","FirstName,LastName,Email,FrequentGuestID,IP Address,Time Stamp Email Marketing","#a#,#b#,#c#,#d#,#e#,#f#",",")#
</cfoutput>
</cfloop>
Output:
test1
test2
test3
test4
test5
Time Stamp test3 Marketing
In the above scenario. The value "Time Stamp Email Marketing" is supposed to be replaced with "test6" but I am getting in an alternative way where it is not replacing the phrase as a whole word. Can anyone tell me how do I replace the list phrases, any alternative for this?
Here you can use the ListQualify function to get exact result of an your scenario. So convert it in to qualify values and looping with that then you can replace it with your own list data. No need to change any order of a list values.
<cfset quoted = listQualify(headerColumnList,"''")>
<cfloop index = "ListElement" list= "#quoted#" delimiters = ",">
#replaceList(ListElement,quoted,"#a#,#b#,#c#,#d#,#e#,#f#")#
<br/>
</cfloop>
The code is working as written. You are seeing this because your check for "Email" in the replaceList() function is firing before the check for "Time Stamp Email Marketing". Notice the word "Email" in that string.
I don't know what your actual use case is but you can change the order of your code for this specific example to make it work like you want.
<cfset headerColumnList = "FirstName,LastName,Email,FrequentGuestID,IP Address,Time Stamp Email Marketing">
<cfset a="test1">
<cfset b="test2">
<cfset c="test3">
<cfset d="test4">
<cfset e="test5">
<cfset f="test6">
<cfloop index = "ListElement" list= "#headerColumnList#" delimiters = ",">
<cfoutput>
#replaceList("#ListElement#","FirstName,LastName,FrequentGuestID,IP Address,Time Stamp Email Marketing,Email","#a#,#b#,#d#,#e#,#f#,#c#",",")#
</cfoutput>
</cfloop>
This gives the desired output. Notice how I reordered the conditions within the replaceList() function.
I am using the below to format the number I get after calulating the % increase or decrease, which means this number can be positive or a negative number. The below works fine on Coldfusion, but on Lucee it throws an error - can't cast [- 6.50] string to a number value. Any idea how to workaround this.
<cfif money_deposit lt 0>
<cfset testVar = abs(NumberFormat(money_deposit,'99.99'))>
<cfelse>
<cfset testVar = NumberFormat(money_deposit,'99.99')>
</cfif>
You should be able to use javaCast
<cfif money_deposit lt 0>
<cfset testVar = abs(NumberFormat(javaCast("float", money_deposit),'99.99'))>
<cfelse>
<cfset testVar = NumberFormat(money_deposit,'99.99')>
</cfif>
First, remove any empty spaces. In any case, it is good practice to first test whether the input parameter is numeric.
<!--- Remove any spaces --->
<cfset money_deposit = REreplace(money_deposit,"\s","","all")>
<cfif isNumeric(money_deposit)>
<cfif money_deposit lt 0>
<cfset testVar = abs(NumberFormat(money_deposit,'-99.99'))>
<cfelse>
<cfset testVar = NumberFormat(money_deposit,'99.99')>
</cfif>
</cfif>
Number format returns a string
http://docs.lucee.org/reference/functions/numberformat.html
The problem is your formatting mask "99.99" means return a formatted number with two characters before the decimal point, the string value you are getting back is "- 6.50", with an extra space. Try using a mask for numberformat of either "9.99" or "09.99"
I have a list of names:
<cfset myList = "John,Joe,Pete">
I want to convert this to a final list that has the string #gmail.com at the end of each name:
<cfoutput> #myList# </cfoutput>
So it will render as:
John#gmail.com,Joe#gmail.com,Pete#gmail.com
All you have to do is simple concatenation in a loop.
<cfset myList = "John,Joe,Pete" />
<cfloop index="item" list="#myList#">
<cfoutput>#item##gmail.com<br /></cfoutput>
</cfloop>
If you have a comma separated list having no spaces in between you can use this also:
<cfset mylist1 = rereplace(mylist, "," , "#gmail.com," , "all") & "#gmail.com" />
Not the best solution but may be helpful in specific cases.
You can try this:
<cfset myList = "John,Joe,Pete">
<cfset newList = reReplace(myList, "(?=,|$)", "#gmail.com", "ALL")>
I'm getting cfquery result like below.This result set for one field value(single line).
Memberof =
"CN=AG-880-ExpenseReports,CN=Users,DC=alcco,DC=com, CN=HTTP Users,CN=Users,DC=alcco,DC=com, CN=WA Houses,CN=Users,DC=alcco,DC=com, CN=MTViewMeadows,CN=Users,DC=alcco,DC=com, CN=ALC0169,CN=Users,DC=alcco,DC=com, CN=ALC0069,CN=Users,DC=alcco,DC=com"
have to take as a list of number ALC0169,ALC0069 from this also i need values only 0169,0069.. from that result set.
Is there a way of doing this with coldfusion?
Here is a very straightforward string processing script that will print out the list of numbers you are looking for based on your description. Where I am printing out the numbers, you will want to capture those into an array or other structure depending on what you want to do with the data.
<cfscript>
memberOf = "CN=AG-880-ExpenseReports,CN=Users,DC=alcco,DC=com,CN=HTTP Users,CN=Users,DC=alcco,DC=com,CN=WA Houses,CN=Users,DC=alcco,DC=com,CN=MTViewMeadows,CN=Users,DC=alcco,DC=com,CN=ALC0169,CN=Users,DC=alcco,DC=com,CN=ALC0069,CN=Users,DC=alcco,DC=com";
memberOf = Replace(memberOf, "CN=Users,DC=alcco,DC=com", "", "all");
memberOf = Replace(memberOf, ",,", ",", "all");
memberOf = Replace(memberOf, "CN=", "", "all");
memberArray = ListToArray(memberOf);
</cfscript>
<cfoutput>
#memberOf#<br/><br/>
<cfloop array="#memberArray#" index="i">
<cfif Left(i, 3) eq "ALC">
#Right(i, Len(i)-3)#<br/>
</cfif>
</cfloop>
</cfoutput>
I would use list functions. Note that you can specify your own delimiter. From your other post, your cfldap tag returned a query named GroupSearch.
<cfset ALCNumbers = "">
<cfloop list = "GroupSearch.MemberOf" Index = "MemberOfThis">
<cfif ListFirst(MemberOfThis, delimiters = "=") is "CN"
and left(ListLast MemberOfThis, delimiters = "="), 3) is "ALC">
<cfset ALCNumbers = ListAppend(ALCNUmbers, mid(ListLast MemberOfThis, delimiters = "="), 4,
len(ListLast MemberOfThis, delimiters = "=") - 3)>
</cfif>
</cfloop>
This may have syntax errors because I simply typed it into the textarea. However, it shows the general idea.
I have the below code:
<cfset abcList = "*,B,b,A,C,a">
<cfset abcList=ListToArray(abcList,',')>
When I output 'abcList' then it is giving me a value but when I use the 'abcList' in <cfif> it's not working. Here is the code which is creating the problem:
<cfoutput>
#abcList[1]# <!---This is giving '*' as Correct o/p--->
<cfif #abcList[1]# eq '*'> <!---Here its going in else--->
list has * at first place
<cfelse>
* is not first
</cfif>
</cfoutput>
Any suggestions on what's wrong in my code?
You don't necessarily need to convert the list to an array. If you are starting from a list variable, you may use Coldfusion list functions to do the same thing without specifying the array conversion.
<cfset abcList = "*,B,b,A,C,a">
<cfif Compare(listGetAt(abcList, 1), '*') EQ 0>
Match
<cfelse>
No Match
</cfif>
Note that most of Coldfusion's string comparisons are not case sensitive. So if you need to test that 'B' is not the same as 'b', you will need to use the compare() function or else use one of the Regular Expression string functions. In this case, compare() returns 0 if string 1 equals string 2. If you do not need case sensitivity, then you may simplify further:
<cfset abcList = "*,B,b,A,C,a">
<cfif listGetAt(abcList, 1) EQ '*'>
Match
<cfelse>
No Match
</cfif>
It also works fine for me. Perhaps you have some extra spaces in the list values? That would skew the results:
<cfset abcList = "#chr(32)#*,B,b,A,C,a">
<cfset abcList=ListToArray(abcList,',')>
<cfoutput>
The value of abcList[1] = #abcList[1]# <br/>
<cfif abcList[1] eq '*'>
list has * at first place
<cfelse>
The else condition was hit because abcList[1] is "(space)*" and not just "*"
</cfif>
</cfoutput>
Try trimming the value first. Also, the # signs around the value are not needed.
<cfif trim(abcList[1]) eq '*'>
....
</cfif>
If that does not work, display the ascii values of both characters. Perhaps they are different than you are thinking.
<cfoutput>
ASCII abcList[1] = #asc(abcList[1])# <br/>
ASCII "*" = #asc("*")# <br/>
</cfoutput>
<cfset abcList = "*,B,b,A,C,a">
<cfset abc=ListToArray(abcList)>
<cfif #abc[1]# eq "*">OK<cfelse>FAIL</cfif>
<cfif abc[1] eq "*">OK<cfelse>FAIL</cfif>
Prints "OK OK" for me. Can you re-confirm it prints something else for you?