如何使用 coldfusion 从字符串中删除单词或字符
How to remove words or chars from a string using coldfusion
我是 coldfusion 的新手,我的目标是根据某些单词删除字符串的一部分。
例如:
<cfset myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure"/>¨
我怎样才能删除单词“与相关的神话之一”以便
有
长城是中国唯一的人造建筑 as string?
我使用了以下函数
RemoveChars(string, start, count)
但我需要使用 RegEx 或本机 coldfusion 函数创建一个函数。
您可以将句子视为由空格分隔的列表。所以如果你想截断你的句子以"Great Wall of China"开头,你可以试试
<cfloop list="#myVar#" index="word" delimiters=" ">
<cfif word neq "Great">
<cfset myVar = listRest(#myVar#," ")>
<cfelse>
<cfbreak>
</cfif>
</cfloop>
<cfoutput>#myVar#</cfoutput>
可能有更快捷的方法。这是 cfLib.org 中的一个函数,它可以以类似的方式更改列表:LINK.
我看到这个问题已经有一个可接受的答案,但我想我会添加另一个答案:)
您可以通过查找单词 'Great' 在字符串中的位置来完成。使用现代 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)", "");
writeDump(substring);
</cfscript>
我是 coldfusion 的新手,我的目标是根据某些单词删除字符串的一部分。
例如:
<cfset myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure"/>¨
我怎样才能删除单词“与相关的神话之一”以便
有
长城是中国唯一的人造建筑 as string?
我使用了以下函数
RemoveChars(string, start, count)
但我需要使用 RegEx 或本机 coldfusion 函数创建一个函数。
您可以将句子视为由空格分隔的列表。所以如果你想截断你的句子以"Great Wall of China"开头,你可以试试
<cfloop list="#myVar#" index="word" delimiters=" ">
<cfif word neq "Great">
<cfset myVar = listRest(#myVar#," ")>
<cfelse>
<cfbreak>
</cfif>
</cfloop>
<cfoutput>#myVar#</cfoutput>
可能有更快捷的方法。这是 cfLib.org 中的一个函数,它可以以类似的方式更改列表:LINK.
我看到这个问题已经有一个可接受的答案,但我想我会添加另一个答案:)
您可以通过查找单词 'Great' 在字符串中的位置来完成。使用现代 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)", "");
writeDump(substring);
</cfscript>