如何用一位数字替换前两位数字 Coldfusion

How to replace the first two digits with one digit Coldfusion

我想把前两位替换成0 如果它以 0 开头,则无需更改任何内容。 示例:445656 到 056....

<cfif number (BEGIN WITH?) "44">
<cfset number = Right(number , Len(number )-2) /> 

但这只会删除前两位数字。谢谢支持

您似乎将“以 0 开头”的数字保留为字符串。试试这个:

<!--- // a number that does not start with zero --->
<cfset theNumber = "445656" />
<cfset theNumber = left(theNumber, 1) is 0 ? theNumber : REReplaceNoCase(theNumber, "^[0-9]{2}", 0) />
<!--- // returns 05656 --->
<cfoutput>#theNumber#</cfoutput>

<hr />

<!--- // a number that starts with zero --->
<cfset theNumber = "05656" />
<cfset theNumber = left(theNumber, 1) is 0 ? theNumber : REReplaceNoCase(theNumber, "^[0-9]{2}", 0) />
<!--- // returns 05656 --->
<cfoutput>#theNumber#</cfoutput>

如果您的电话号码总是以 0 或 44 开头,您可以使用

<cfset theNumber = left(theNumber, 1) is 0 
                    ? theNumber 
                    : REReplaceNoCase(theNumber, "^[4]{2}", 0) /> 

Update: Also, read the comments, there are some good points in there.

这个 reReplace() 可以 运行 任何字符串,如果字符串以 44 开头,只会修改并替换为 0。

<cfscript>
   phoneNumber = "44556677";
   phoneNumber = reReplace(phoneNumber, "^44", "0");
   writeOutput(phoneNumber);
</cfscript>