This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes help required with split method of String class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "help required with split method of String class" Watch "help required with split method of String class" New topic
Author

help required with split method of String class

Bilal Ali
Ranch Hand

Joined: Jun 14, 2007
Posts: 66
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.
vijay mahendra
Ranch Hand

Joined: Jan 01, 2007
Posts: 51
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

Joined: Jun 14, 2007
Posts: 66
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.
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi ,

Use \\. in place of \.


Regards, Abdul Mohsin
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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 "."


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: help required with split method of String class
 
Similar Threads
StringTokenizer question
Converting a double value to a fraction
parsing
split method in string
How we can Count the no.of words in a given String