Reputation: 177
I need to do to a String manipulation. Initially I will be getting an image path as one of the below:
image = images/registration/student.gif
image = images/registration/student_selected.gif
image = images/registration/student_highlighted.gif
and I need to manipulate the string image path to get 2 different image paths.
One is to get the path as:
image1 = images/registration/student.gif
for that I used the function below:
private String getImage1(final String image) {
String image1 = image;
image1 = image.replace("_highlight", "");
image1 = image.replace("_selected", "");
return image1;
}
the second image path I need is to get the path:
image2 = image = images/registration/student_selected.gif
the function I used to get the image2
output was:
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;
}
But the above methods didn't give me the expected result. Can anyone help me with it? Thanks a lot!
Upvotes: 0
Views: 89
Reputation: 3004
If I understood correctly, you need below outputs from respective functions
"images/registration/student.gif"-> getImage1(String)
"images/registration/student_selected.gif" -> getImage2(String)
Assuming above output, there are few mistakes in the both functions getImage1()->
getImage2()->
I have modified the functions as below which gives required output
private static String getImage1(final String image) {
return image.replace("_highlighted", "").replace("_selected", "");
}
private static String getImage2(final String image) {
if (image.indexOf("_")!=-1) {
return image.replace("highlighted", "selected");
} else {
return image.replace(".", "_selected.");
}
}
Upvotes: 1
Reputation: 46209
1.
private String getImage1(final String image) {
return image.replaceAll("_(highlighted|selected)", "");
}
2.
private String getImage2(final String image) {
return image.replaceAll(getImage1(image).replaceAll("(?=.gif)", "_selected");
}
Upvotes: 0
Reputation: 1454
First, getImage1() is probably not doing what you want it to do. You are assigning a value to the variable image1 3 times. Obviously the last assigned value gets returned.
Second, image2.matches("_")
will not tell you if image2 contains an underscore (which I think is what you're trying to do here).
I suggest to do some more testing/debugging yourself first.
Upvotes: 0
Reputation: 195039
you could use indexOf(...) instead of match(). match will check the whole string against the regex.
for (final String image : new String[] { "images/registration/student.gif", "images/registration/student_highlight.gif",
"images/registration/student_selected.gif" }) {
String image2 = image;
final boolean hasUndersore = image2.indexOf("_") > 0;
if (hasUndersore) {
image2 = image2.replaceAll("_highlight(\\.[^\\.]+)$", "_selected$1");
} else {
final String[] words = image2.split("\\.");
image2 = words[0].concat("_selected.") + words[1];
}
System.out.println(image2);
}
this will give you expected output.
btw, i changed replaceAll(..) regex, since the image filename could have string "highlight" as well. e.g. stuhighlight_highlight.jpg
Upvotes: 2