• 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

help required with split method of String class

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
We have a requirement that the user enters some decimal number like 123.345 or 12.0987 , now we want to break the real part and fractional part of the string , like for example when 123.345 is breaked it becomes "123" and "345" . I am using split method of the string class to break the real part and the fractional part. I am doing is follows:



the resultant array should be having 2 indexes but the array obtained after splitting is empty which means that it found no "." , am I making any mistake , please guide us that what to do.

Regards,
Bilal Ali.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
you need to try as


"." default operator ,so we should use escape character before that.
Now it will work

Thanks,
Vijay
 
Bilal Ali
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friend,
Thanks for your reply however whenever I used your tecqnique "\." I am getting compile time error that "illegal escape character" , can you please help.
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Use \\. in place of \.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To explain:

the . is a special character in regex that matches any single character, thus your empty array

the '\.' will tell the regex to escape that behavior and actually match the .

but within a java string, the \ is an escape character as well, so to get a \ in the string so that it is passed to regex (from within split) we need to escape, yet again, the \\ will give us the \ in the string, thus

"\\." will be compiled into "/." which will be interpreted as "."
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic