File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes extracting a portion of number from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "extracting a portion of number from a string" Watch "extracting a portion of number from a string" New topic
Author

extracting a portion of number from a string

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi Folks,

I have to extract the account # from the barcode that is if LYL01016278572011000000012 is a complete string ,then account No. 100000001

complete barcode LYL01016278572011000000012
account no 100000001

Please advise me how java sply thruough string functionality I can achieve this ...will matcher and regex will help ..!!!
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 193

I think you must be having fix pattern like first 12 characters denotes XXX then next 6 characters denotes YYY then last 2 characters denotes ZZZ.
If this is the case then you can use the subString method to extract the info from/to particular index.


Do not wait to strike till the iron is hot; but make it hot by striking....
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Saral Saxena wrote:Hi Folks,

I have to extract the account # from the barcode that is if LYL01016278572011000000012 is a complete string ,then account No. 100000001

complete barcode LYL01016278572011000000012
account no 100000001

Please advise me how java sply thruough string functionality I can achieve this ...will matcher and regex will help ..!!!


Yeah please suggest me ...I am stuck up at this prblm...!!can we use regex or some other thing..!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Saral Saxena wrote:
Yeah please suggest me ...I am stuck up at this prblm...!!can we use regex or some other thing..!!


What's wrong with using the substring() method as already mentioned?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Henry Wong wrote:
Saral Saxena wrote:
Yeah please suggest me ...I am stuck up at this prblm...!!can we use regex or some other thing..!!


What's wrong with using the substring() method as already mentioned?

Henry


Nothing wrong with it ..implementation would be some how as...



But does java 6 offers some other advanced functions also..!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Saral Saxena wrote:
Nothing wrong with it ..implementation would be some how as...



But does java 6 offers some other advanced functions also..!!



Of course, it is possible to use regex, or other advanced techniques to do a substring -- but why?

Henry
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Henry Wong wrote:
Saral Saxena wrote:
Nothing wrong with it ..implementation would be some how as...



But does java 6 offers some other advanced functions also..!!



Of course, it is possible to use regex, or other advanced techniques to do a substring -- but why?

Henry


could we use in this case as I want to explore, please it wil be a great help if you can show me how the above code can be done through regex..!! thanks in advance..!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Why would you like a more advanced solution if this simple solution almost works? I'm saying almost because your code will throw an exception if the String is too short. Instead check for the length:
One more improvement is to get rid of these magic numbers:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Saral Saxena wrote:
could we use in this case as I want to explore, please it wil be a great help if you can show me how the above code can be done through regex..!! thanks in advance..!!



A bit of a weird request, but why not?

In your code, replace this line....



with this one....



Henry
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Henry Wong wrote:
Saral Saxena wrote:
could we use in this case as I want to explore, please it wil be a great help if you can show me how the above code can be done through regex..!! thanks in advance..!!



A bit of a weird request, but why not?
In your code, replace this line....

with this one....

Henry


Hi Henry,

Thanks a lot , i will follow your advise..!! thanks a lot ...it will be a great help if you could please specify...

the above regular expression is also doing the same thing that earlier I WAS DOING..THOUGH IT IS MORE PERFECT..!! but I am unable to grasp that regular expression could you please explain it that will make more clear..!1thanks in advance..!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Related to your other topic...

http://www.coderanch.com/t/572089/java/java/masked-String

If you want to mask out the number, you can do this line instead...



Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40



Oops. I didn't read the request clearly... to mask out all but the last four, use this line....




Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: extracting a portion of number from a string