我需要对字符串进行操作。最初,我将获得如下之一的图像路径:
image = images/registration/student.gif
image = images/registration/student_selected.gif
image = images/registration/student_highlighted.gif
我需要操纵字符串图像路径以获得2个不同的图像路径。
一种是获取路径:
image1 = images/registration/student.gif
为此,我使用了以下功能:
private String getImage1(final String image) {
String image1 = image;
image1 = image.replace("_highlight", "");
image1 = image.replace("_selected", "");
return image1;
}
我需要的第二个图像路径是获取路径:
image2 = image = images/registration/student_selected.gif
我用来获取image2
输出的函数是:
private String getImage2(final String image) {
String image2 = image;
boolean hasUndersore = image2.matches("_");
if (hasUndersore) {
image2 = image2.replace("highlight", "selected");
} else {
String[] words = image2.split("\\.");
image2 = words[0].concat("_selected.") + words[1];
}
return image2;
}
但上述方法并没有给我预期的结果。有人可以帮我吗?非常感谢!