• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extracting a portion of number from a string

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Henry Wong
author
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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




Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic