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 Removing dahes out of string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Removing dahes out of string" Watch "Removing dahes out of string" New topic
Author

Removing dahes out of string

john mccain
Greenhorn

Joined: Jan 03, 2010
Posts: 6
Hey guys. first post. im stuck and i cant figure out how to remove dashes out of an input string that MAY or MAY NOT have dashes. Would i be looking to use char or what? Also another thing is im trying to not use arrays.

Here is my method header:

Would love some help please.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1430

Have a look at String's replaceAll() method.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
john mccain
Greenhorn

Joined: Jan 03, 2010
Posts: 6
Jelle Klap wrote:Have a look at String's replaceAll() method.


Alright i looked at that and i got this far...

Now what would i put in the XXX to make it remove it completely?

OR how do i use replaceAll for this situation? i need to completely get rid of dashes for an isbn method.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That compiles?!

In any case: what do you want to replace it with? Nothing, right? So... what's string-ese for nothing?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2816
    
    2
What's this "char 45" stuff, anyway?

Note that in the API the Jelle linked to, there's a replace() method, and a replaceAll(). Pay attention to the difference.

Good to see you're keeping busy after the election, sir. Good luck.
john mccain
Greenhorn

Joined: Jan 03, 2010
Posts: 6
OKAY OKAY. semi success. My code now:


So now it works but i need to make it to work weather it has or doesnt have dashes. Any help while i try stuff?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Seeing as how there's no "remove()" method, consider replacing. Again--with what? Your answer should be "nothing". Again--what's String-eze for nothing?
john mccain
Greenhorn

Joined: Jan 03, 2010
Posts: 6
David Newton wrote:Seeing as how there's no "remove()" method, consider replacing. Again--with what? Your answer should be "nothing". Again--what's String-eze for nothing?


its "". lol i got that. i am now making a if-else statement to test if it has dashes, remove dashes. else do nothing....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why bother? Wouldn't anything doing any replacing have to check anyway?
john mccain
Greenhorn

Joined: Jan 03, 2010
Posts: 6
oh hold on.. lemme try something i mighta missed :P
john mccain
Greenhorn

Joined: Jan 03, 2010
Posts: 6
yeah i got it!! thanks alot guys. ill be sure to stick around this forum.
 
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: Removing dahes out of string
 
Similar Threads
OOP-1 question
String - Remove dashes from text
Database configuration
loop help?
String method "replace(char, char)"