Reputation: 451
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?
Upvotes: 0
Views: 204
Reputation: 14333
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
Upvotes: 5
Reputation: 31912
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>
Upvotes: 0