0
基本上我必须在groovy上设计和实现这个,这是编码和解码一个特定的段落?如何为字母替换密码(groovy)创建编码器和解码器?
基本上我必须在groovy上设计和实现这个,这是编码和解码一个特定的段落?如何为字母替换密码(groovy)创建编码器和解码器?
你可以看看这个
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)
哪一部分是你有问题吗? – hvgotcodes 2011-01-06 20:04:39
基本上,我只是想要开始代码的结构。 – ben 2011-01-06 20:17:28