|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-20-2008, 04:10 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 55
|
|
|
How can i get substring
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 ?
|
|

06-20-2008, 04:14 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-20-2008, 06:36 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 338
|
|
In order to extract the substring from a string,you can use the split method
So this is what you really need i think:
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]);}
}
And the output will be:
I hope i helped you.
Last edited by serjant : 06-23-2008 at 05:33 PM.
|
|

06-23-2008, 11:38 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
But this is failed if the path too long. I mean number of slashes issue. Since the path define is unchanged this is fine. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-23-2008, 12:09 PM
|
 |
Member
|
|
Join Date: Jun 2008
Location: China
Posts: 3
|
|
|
serjant
doing great!
__________________
Java is my life!live java!
|
|

06-23-2008, 12:20 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-25-2008, 11:40 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 11
|
|
It depends on how much you know about the path before hand.
A good way of doing this is with the regex package:
import java.util.regex.*;
Then you would create a pattern and a matcher:
//(.*) - 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.
|
|

06-26-2008, 04:27 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
Here is my solution ^_^
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 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 SHPTH7009
I hope that helps
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

06-26-2008, 05:45 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Originally Posted by Eku
Here is my solution ^_^
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 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 SHPTH7009
I hope that helps
I give my points to this. Best way to do it. Reason to use this is, no need to import addition packages to my application. So that package linker times is reduce. I really like that.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 05:52 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Originally Posted by c26354
It depends on how much you know about the path before hand.
A good way of doing this is with the regex package:
import java.util.regex.*;
Then you would create a pattern and a matcher:
//(.*) - 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.
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 06:10 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
|
thanks for the appreciation eranga. I had met this kind of problem before. Experience really teaches me here ^_^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

06-26-2008, 06:29 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 05:40 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 11
|
|
|
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.
|
|

06-27-2008, 03:54 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
|
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.
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

06-27-2008, 06:35 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
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. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|