2011-01-06 39 views

回答

0

你可以看看這個

http://groovy.codehaus.org/ExpandoMetaClass+-+Dynamic+Method+Names

其中顯示了編解碼器的典型使用情況。

基本上類似(從該鏈接)

class HTMLCodec { 
    static encode = { theTarget -> 
     HtmlUtils.htmlEscape(theTarget.toString()) 
    } 

    static decode = { theTarget -> 
     HtmlUtils.htmlUnescape(theTarget.toString()) 
    } 
} 

你不會使用HtmlUtils,但結構是一樣的。

編輯 - 這裏是一個關於如何做替換的例子。請注意,這可能可以更時髦,而且它並不處理標點符號,但它應該幫助

def plainText = 'hello' 
def solutionChars = new char[plainText.size()] 
for (def i = 0; i < plainText.size(); i++){ 
     def currentChar = plainText.charAt(i) 
     if (Character.isUpperCase(currentChar)) 
       solutionChars[i] = Character.toLowerCase(currentChar) 
     else 
       solutionChars[i] = Character.toUpperCase(currentChar) 

} 

def cipherText = new String(solutionChars) 
println(solutionChars) 

編輯 - 這裏是一個解決方案,是一個比較常規

def plainText = 'hello' 
def cipherText = "" 
plainText.each {c -> 
    if (Character.isUpperCase((Character)c)) 
     cipherText += c.toLowerCase() 
    else 
     cipherText += c.toUpperCase() 
} 

println(cipherText)