2011-01-06 36 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)