2016-08-17 2 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

나는이 질문은 이미 허용 대답을 가지고 볼 수 있지만, 나는 당신이 단어를 '위대한'는 문자열입니다 찾아 그것을 할 수있는 또 다른 답 :

를 추가 할 거라고 생각했다. 현대 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> 

당신이 양쪽 오프 문자를 차단하려는 경우 중간 당신에게 좀 더 유연성을 제공 할 사용. 로 기록 될 것이다 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>