2016-08-17 3 views
1

Ich bin neu bei Coldfusion und mein Ziel ist es, einen Teil einer Zeichenfolge nach bestimmten Wörtern zu entfernen.So entfernen Sie Wörter oder Zeichen aus einer Zeichenfolge mit Coldfusion

Zum Beispiel:

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

Wie kann ich die Worte „Einer der Mythen im Zusammenhang mit dem“ entfernen, um haben
Great Wall of China ist, dass es das einzige ist, künstliche Struktur als String?

habe ich folgende Funktion

RemoveChars(string, start, count) 

Aber ich brauche eine Funktion, vielleicht mit RegEx oder native Coldfusion-Funktionen zu erstellen.

Antwort

0

Sie könnten den Satz als eine durch Leerzeichen getrennte Liste sehen. Also, wenn Sie Ihren Satz abzuschneiden mit „Great Wall of China“ zu starten, könnten Sie

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

versuchen Es kann ein schneller Weg, dies zu tun. Hier ist eine Funktion bei cfLib.org, die eine Liste auf ähnliche Weise ändern kann: LINK.

4

Ich sehe diese Frage bereits eine Antwort akzeptiert hat, aber ich dachte, dass ich eine andere Antwort hinzufügen würde :)

Sie es, indem ich tun können, wo das Wort ‚Großes‘ ist in der Zeichenkette. Mit modernen CFML können Sie es wie folgt:

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

Mit Mitte würde Ihnen ein bisschen mehr Flexibilität, wenn Sie aus den beiden Enden schneiden Zeichen wollen.

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

In älteren Versionen von CF, die als geschrieben werden würde:

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

Sie auch einen regulären Ausdruck verwenden, könnte das gleiche Ergebnis zu erzielen, werden Sie entscheiden, welche in geeigneter ist Ihre Anwendungsfall:

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