如何将一个字符串转换成一段代码(Factory Method Pattern?) [英] How to convert a string into a piece of code (Factory Method Pattern?)

查看:129
本文介绍了如何将一个字符串转换成一段代码(Factory Method Pattern?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个这样的字符串:

  String string2code =variable ='hello';; 

我们如何将该字符串转换成这样一段代码?
$ b

  variable =hello; 


解决方案

GroovyShell 是答案:

  String string2code =variable ='hello'; return variable.toUpperCase(); 

def result = new GroovyShell()。evaluate string2code
assert result ==HELLO


Let's say we have a String like this:

String string2code = "variable = 'hello';";

How could we convert that String to a piece of code like this?:

variable = "hello";

解决方案

GroovyShell is the answer:

String string2code = "variable = 'hello'; return variable.toUpperCase()";

def result = new GroovyShell().evaluate string2code
assert result == "HELLO"

这篇关于如何将一个字符串转换成一段代码(Factory Method Pattern?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆