Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > replace period using String.replaceAll()

Reply
Thread Tools

replace period using String.replaceAll()

 
 
sfu
Guest
Posts: n/a
 
      09-24-2003
Hi

i'm trying to remove all occurances of . from a string

test = test.replaceAll(".","");

and

int fs = 46;
Character fullstop = new Character((char) fs);
test = test.replaceAll(fullstop.toString(),"");

both dont work, any ideas on how to get it to work?

cheers
 
Reply With Quote
 
 
 
 
secret
Guest
Posts: n/a
 
      09-24-2003
It appears that your problem is that the period has a defined meaning within
regular expression. This is not replacing a substring in the way you
imagine.

Try putting two backslashes in front of the period. One backslash is needed
to escape the char in the regular expression. The other is needed to escape
the first backslash in Java.

test = test.replaceAll("\\.","");


"sfu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi
>
> i'm trying to remove all occurances of . from a string
>
> test = test.replaceAll(".","");
>
> and
>
> int fs = 46;
> Character fullstop = new Character((char) fs);
> test = test.replaceAll(fullstop.toString(),"");
>
> both dont work, any ideas on how to get it to work?
>
> cheers



 
Reply With Quote
 
 
 
 
secret
Guest
Posts: n/a
 
      09-24-2003
I just reread my post and decided it was quite unitelligible (though the
code works).

The essence of your problem is that the method replaceAll in String does not
replace a substring with the replacement string the way you might
intuitively imagine. Chech the API and you'll see that the first argument
is a regular expression, not a substring of the target.

good luck

alan

"sfu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi
>
> i'm trying to remove all occurances of . from a string
>
> test = test.replaceAll(".","");
>
> and
>
> int fs = 46;
> Character fullstop = new Character((char) fs);
> test = test.replaceAll(fullstop.toString(),"");
>
> both dont work, any ideas on how to get it to work?
>
> cheers



 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      09-25-2003
secret wrote:
> I just reread my post and decided it was quite unitelligible (though the
> code works).
>
> The essence of your problem is that the method replaceAll in String does not
> replace a substring with the replacement string the way you might
> intuitively imagine. Chech the API and you'll see that the first argument
> is a regular expression, not a substring of the target.



The part that's far less clear from the documentation is that the second
parameter isn't just a String either; it's a pseudo-regexp where most
special characters don't work, but subexpression references do. That
means, essentially, that you have to be sure to properly escape BOTH
parameters to this method (and in consequence, the method is practically
useless unless both parameter values are known when the code is
written).

In the OP's case, that's not a problem, though. Just a gotcha to look
out for.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-25-2003
On 24 Sep 2003 15:53:08 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (sfu) wrote or
quoted :

>test = test.replaceAll(".","");


replaceAll is a regex method masquerading as a humble String method.

.. has magic meaning in regexs.

See http://mindprod.com/jgloss/regex.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
D Goldman
Guest
Posts: n/a
 
      09-25-2003
Hello, try test = test.replaceAll("[.]","");

Hope this helps,
Daniel Goldman
http://d-goldman.org


(E-Mail Removed) (sfu) wrote in message news:<(E-Mail Removed). com>...
> Hi
>
> i'm trying to remove all occurances of . from a string
>
> test = test.replaceAll(".","");
>
> and
>
> int fs = 46;
> Character fullstop = new Character((char) fs);
> test = test.replaceAll(fullstop.toString(),"");
>
> both dont work, any ideas on how to get it to work?
>
> cheers

 
Reply With Quote
 
sfu
Guest
Posts: n/a
 
      09-25-2003
"secret" <(E-Mail Removed)> wrote in message news:<qLpcb.9213$(E-Mail Removed) om>...
> It appears that your problem is that the period has a defined meaning within
> regular expression. This is not replacing a substring in the way you
> imagine.
>
> Try putting two backslashes in front of the period. One backslash is needed
> to escape the char in the regular expression. The other is needed to escape
> the first backslash in Java.
>
> test = test.replaceAll("\\.","");


cheers mate!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using scriptaculous, how to trigger mouseover effect ONLY if mousedover for a period of time AndrewC Javascript 2 04-30-2008 03:13 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
VoIPCheap/Stunt/SIPDiscount/Et.al - Mobile - Top-up Expiry Period -- Campaign for Correct Expiry Period on Finarea VOIP Service Mobile Top-Ups News Reader UK VOIP 16 06-26-2006 05:03 PM
replace text in IFRAME using innerHTML.replace(...) possible? mscir Javascript 0 10-11-2005 11:21 PM
Finding period/dot (.) in HTML form input using regular expressions Sudhakar Doddi Javascript 1 08-22-2003 06:50 PM



Advertisments