• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

extracting a portion of number from a string

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..!!!
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..!!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Related to your other topic...

https://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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic