0

基本上我必须在 groovy 上设计和实现它,这是对特定段落的编码和解码?

4

1 回答 1

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)
于 2011-01-06T20:07:43.977 回答