2016-08-17 7 views
1

私はcoldfusionを初めて使用しています。目標は特定の単語に基づいて文字列の一部を削除することです。例えばcoldfusionを使用して文字列から単語や文字を削除するには


<cfset myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure"/>¨ 

がどのように私が持っている するために言葉「に関連した神話のワン」を削除することができ
中国の万里の長城はそれだけであるということです人工構造を文字列として使用しますか?

私は

RemoveChars(string, start, count) 

次の関数を使用しかし、私は正規表現またはネイティブのColdFusion関数で多分関数を作成する必要があります。

答えて

0

スペースで区切られたリストとしてその文を見ることができます。ですから、「中国の万里の長城」で開始するには、あなたの文を遮断したい場合、あなたは

<cfloop list="#myVar#" index="word" delimiters=" "> 
    <cfif word neq "Great"> 
    <cfset myVar = listRest(#myVar#," ")> 
    <cfelse> 
    <cfbreak> 
    </cfif> 
</cfloop> 
<cfoutput>#myVar#</cfoutput> 

を試みることができる、これを行うための迅速な方法があるかもしれません。同様の方法でリストを変更できるcfLib.orgの関数は次のとおりです:LINK

4

私はこの質問が既に受け入れ答えを持っていますが、私は別の答えを追加しようと思いました:)

あなたが単語「大」の文字列である場合見つけることによってそれを行うことができます参照してください。あなたは両端オフ文字をカットしたい場合は半ばはあなたにもう少し柔軟性を与えるだろう使用

<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> 

:現代CFMLを使用すると、そのようにそれを行うことができます。以下のように記述されるだろう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 = myVar.FindNoCase("Great"); 

// get the substring 
substring = myVar.mid(a, myVar.len()); 
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"; 

// 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> 
関連する問題