There is String
a = "/store/assets/images/products/Rings/SHPTH7009_OV_1_Small.jpg";
I want to get 'SHPTH7009' from this string .How can i get ?
Printable View
There is String
a = "/store/assets/images/products/Rings/SHPTH7009_OV_1_Small.jpg";
I want to get 'SHPTH7009' from this string .How can i get ?
In order to extract the substring from a string,you can use the split method
So this is what you really need i think:
And the output will be:Code:public class Shell {
private String string="/store/assets/images/products/Rings/SHPTH7009_OV_1_Small.jpg";
private String[] split=string.split("[/_]");
public static void main(String[] args) {
System.out.println(new Shell().split[6]);}
}
I hope i helped you.Code:SHPTH7009
But this is failed if the path too long. I mean number of slashes issue. Since the path define is unchanged this is fine. ;)
serjant
doing great!
Yes I agreed with that. I'm not saying it is bad practice at all. I'm saying is what happened if the pattern of the path is changed.
It depends on how much you know about the path before hand.
A good way of doing this is with the regex package:
Then you would create a pattern and a matcher:Code:import java.util.regex.*;
Code://(.*) - the .* means anything and the () means a capture group
Pattern pat = Pattern.compile("/(.*).jpg");
Matcher match = pat.matcher(a); //a is the string to match
String fileName = match.group(1); //this will return everything from the (.*)
now you have the file name. The advantage of doing this is that you can be way more specific about what part to get. If you don't want the part after the _ then use the split method given by serjant.
Here is my solution ^_^
I cut the left side of the string from the last '/' including the slash and cut the rigth side of the String from '_' to the last and thats how i extracted the SHPTH7009Code:String a = "/store/assets/images/products/Rings/SHPTH7009_OV_1_Small.jpg";
String temp = a.substring(a.lastIndexOf('/')+1);
temp = temp.substring(0, temp.indexOf('_'));
System.out.println(temp);
I hope that helps
So importing another package you have to find the file name first. Then you have to split it again to find the correct string. Why you use such a long way, for me this is too much for such a simple thing.
And also for me, your pattern is wrong. I don't think it can find the file name. Check the pattern (.*) there.
thanks for the appreciation eranga. I had met this kind of problem before. Experience really teaches me here ^_^
I'm cool here Eku. First thing is really like to give what I know to any other person. I love that. If I feel that something is good, in my experience, I appreciate it. :)
That mean experience is the best in your life, plays a major role.
Good catch, the (.*) will be greedy, so you would need (.*?) instead.
The (.*?) will give you "SHPTH7009_OV_1_Small". Instead of that you could make the pattern "/(.*?)_" and it will get only "SHPTH7009".
Of course its better if you don't have to use any other packages, but this is a very general way of doing this, and it give lots of flexibility. I guess its mostly only useful if you don't know a lot about the file. For example, what if there is a directory that contains a "_". That might cause problems.
Regex is a helpful package to know, but not needed in this case.
yeah got your point c26354 ^_^. There are a total of two cutting process.
1. Eliminating the directory from the String
2. Getting the substring from index 0 of Temp to the index of the first "_" in Temp
So there will no problem if there is a "_" in the directory it will still cut the from the last '/'. The only problem i see in that is if there is a '/' in the filename which is not that usual.
But still regex is much useful in most complex cases but not in this one.
c26354 and Eku,
As both of you said, regex is much better much useful package in Java, not in a simple project like this. Regular expressions used in depth in complex projects.
Such a simple case, it's useless for me. As I said in my last post, to get the correct information you have to do few things. For me it's too much. :)