| 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);
|
 |
 |
|
|
subject: help required with split method of String class
|
|
|