ColdFusion - String删除特定点后的字符

时间:2014-11-27 21:14:24

标签: string coldfusion coldfusion-9

字符串长度始终如下格式 - 但长度不同。

地址:BLLAHHH Blahhh Blahhhhh Blahhh Det:HSD03 Evt:F2014999999注意 - 本通讯仅适用于上述人员或实体,可能包含机密或法律特权的信息。 Blahhh Blahhh Blahhh

我试图只获得以数字结尾

结尾的数据

地址:BLLAHHH Blahhh Blahhhhh Blahhh Det:HSD03 Evt:F2014999999

我尝试了各种各样的字符串函数并没有太多运气。

下面的代码让我:

地址:BLLAHHH Blahhh Blahhhhh Blahhh Det:HSD03 Evt:F2014999999通知

     <cfset string = "#textbody#">
     <br>
     <cfset firstPiece = listGetAt(string, 1, "-")>
     [ [ <cfoutput>#firstPiece#</cfoutput> ] ]

3 个答案:

答案 0 :(得分:3)

function trimToLastEndWithDigit(str) {
    var noticePos = find(" NOTICE -", str);
    return left(str, noticePos);
}

str = "Add: BLLAHHH Blahhh Blahhhhh Blahhh Det: HSD03 Evt: F2014999999 NOTICE - This communication is intended ONLY for the use of the person or entity named above and may contain information that is confidential or legally privileged. Blahhh Blahhh Blahhh";

writeOutput(trimToLastEndWithDigit(str));

http://www.trycf.com/scratch-pad/pastebin?id=c9N8NEUD

我之前尝试使用正则表达式来满足您的需求,但您需要的实际上更简单,所以请忽略函数名称。

答案 1 :(得分:1)


您可以按如下方式使用正则表达式:
你的字符串:

<cfset testString = "Add: BLLAHHH Blahhh Blahhhhh Blahhh Det: HSD03 Evt: F2014999999 NOTICE - This communication is intended ONLY for the use of the person or entity named above and may contain information that is confidential or legally privileged. Blahhh Blahhh Blahhh"/>

正则表达式:

<cfset testSt = ReReplace(testString,"NOTICE.*","")>

输出:

Add: BLLAHHH Blahhh Blahhhhh Blahhh Det: HSD03 Evt: F2014999999

答案 2 :(得分:0)

我认为......

TextBody是:

地址:BLLAHHH Blahhh Blahhhhh Blahhh Det:HSD03 Evt:F2014999999注意 - 本通讯仅适用于上述人员或实体,可能包含机密或法律特权的信息。 Blahhh Blahhh Blahhh

    <cfset fp = "#textbody#" />

    <cfoutput>#REReplace(fp, "NOTICE(.*)", "NOTICE")#</cfoutput>
    <cfset newstr = "#REReplace(fp, "NOTICE(.*)", "NOTICE")#">

    <cfset final = left(newstr, len(newstr) -7)>

     [<b>#final#</b>]