当我的程序运行时,我不知道如何用字符串替换字符串。这有可能吗?当程序运行时,我想用一个单词替换另一个单词,然后从该行继续运行。
我怎么能这样做呢?感谢所有人
hstring.replace("picstart", "up1");
g.drawPixmap(Assets.picstart , 128, 160);
发布于 2013-01-24 20:13:16
您的代码将如下所示。
if (condition) {
g.drawPixmap(Assets.picstart , 128, 160);
} else {
g.drawPixmap(Assets.up1 , 128, 160);
}
发布于 2013-01-24 20:11:27
String
是不可变的,这意味着您必须将结果赋给一个变量:
hstring = hstring.replace(...);
发布于 2013-01-24 20:16:08
不要使用反射。假设您的资源代码处理您的图像、纹理或其他任何情况:
String hstring = "picstart";
// ... stuff happens
// that forces us to change hstring! ...
hstring = hstring.replace("up1"); // or you could just say hstring = "up1";
g.drawPixmap(Assets.getAssetFor(hstring), 128, 160);
然后,在static Asset类中,您可以:
public PixelMap getAssetFor(String identifier) {
if (identifier.equals("picstart") {
return new PicStartPixelMap();
}
else if (identifier.equals("up1")) {
return new UpOnePixelMap();
}
}
https://stackoverflow.com/questions/14509830
复制相似问题