我是coldfusion的新手,我的目标是根据某些词语删除部分字符串。
例如:
<cfset myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure"/>¨
如何删除单词&#34; 与&#34;相关的其中一个神话?为了 有 中国的长城是它是唯一的人造结构如弦?
我使用了以下功能
RemoveChars(string, start, count)
但我需要使用RegEx或本机coldfusion函数创建一个函数。
答案 0 :(得分:4)
我看到这个问题已经有了一个可以接受的答案,但我想我还要添加另一个答案:)
你可以通过查找单词&#39; Great&#39;的位置来实现。在字符串中。使用现代CFML,您可以这样做:
<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>
如果你想削减两端的字符,使用mid会给你更多的灵活性。
<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>
在旧版本的CF中,可写为:
<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>
您也可以使用正则表达式来获得相同的结果,您必须决定哪个更适合您的用例:
<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>
答案 1 :(得分:0)
您可以将句子视为以空格分隔的列表。所以,如果你想切断你的句子,开始使用&#34;中国长城&#34;,你可以试试
<cfloop list="#myVar#" index="word" delimiters=" ">
<cfif word neq "Great">
<cfset myVar = listRest(#myVar#," ")>
<cfelse>
<cfbreak>
</cfif>
</cfloop>
<cfoutput>#myVar#</cfoutput>
可能有更快的方法来做到这一点。这是cfLib.org上的一个功能,它可以以类似的方式更改列表:LINK。