Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-20-2008, 04:10 PM
Member
 
Join Date: Jun 2008
Posts: 55
82rathi.angara is on a distinguished road
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 ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-20-2008, 04:14 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
substring(int nodestart,int nodeEnd)
__________________
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-20-2008, 06:36 PM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 338
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
In order to extract the substring from a string,you can use the split method
So this is what you really need i think:
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]);} }
And the output will be:
Code:
SHPTH7009
I hope i helped you.

Last edited by serjant : 06-23-2008 at 05:33 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-23-2008, 11:38 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-23-2008, 12:09 PM
ilysony's Avatar
Member
 
Join Date: Jun 2008
Location: China
Posts: 3
ilysony is on a distinguished road
Send a message via MSN to ilysony
serjant
doing great!
__________________
Java is my life!live java!
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-23-2008, 12:20 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-25-2008, 11:40 PM
Member
 
Join Date: Jun 2008
Posts: 11
c26354 is on a distinguished road
It depends on how much you know about the path before hand.

A good way of doing this is with the regex package:

Code:
import java.util.regex.*;
Then you would create a pattern and a matcher:

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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-26-2008, 04:27 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
Here is my solution ^_^

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-26-2008, 05:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by Eku View Post
Here is my solution ^_^

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-26-2008, 05:52 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by c26354 View Post
It depends on how much you know about the path before hand.

A good way of doing this is with the regex package:

Code:
import java.util.regex.*;
Then you would create a pattern and a matcher:

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.
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-26-2008, 06:10 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-26-2008, 06:29 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-26-2008, 05:40 PM
Member
 
Join Date: Jun 2008
Posts: 11
c26354 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 06-27-2008, 03:54 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 06-27-2008, 06:35 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read-File Write Display substring hiklior New To Java 3 04-18-2008 01:45 PM
How to use subString method Java Tip java.lang 0 04-17-2008 09:44 PM
help me with a realy easy program (substring) michcio New To Java 7 01-27-2008 02:41 AM
String substring function ravian New To Java 6 01-02-2008 09:35 PM
substring Java Tip Java Tips 0 11-11-2007 10:15 PM


All times are GMT +3. The time now is 09:46 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org