aspose file tools*
The moose likes Beginning Java and the fly likes Stripping characters from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stripping characters from a string" Watch "Stripping characters from a string" New topic
Author

Stripping characters from a string

David Crane
Greenhorn

Joined: Aug 29, 2009
Posts: 15
hey, I need a bit of help managing to strip non-numeric characters from a string, if I remember right, I use a boolean and char app for this, but I can't get it right so I was hoping someone here would be able to give me a little help with this.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
nothing Swing-specific to do with the problem,
but a regular expression would do the job
David Crane
Greenhorn

Joined: Aug 29, 2009
Posts: 15
could you give me an example of the code I would use to do this, Michael?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> could you give me an example of the code I would use to do this, Michael?

I could, but the beginners forum is a 'you give it a go first, we'll correct it'

look up the regex package (or google java regular expression)

common gotcha is the escape character \ often is required as \\
David Crane
Greenhorn

Joined: Aug 29, 2009
Posts: 15
I would give it a go, the problem is I don't know how

I have my string in a boolean form now



what I can't figure out is how to tell it to remove non-numeric characters
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

I am really really confused as to why you think that stripping characters from a string has anything to do with Boolean.

If regular expressions are a bit beyond your grasp at this point, just do it the old-fashioned way. What steps would you go through to accomplish this task?

If you have a hard time starting with that, let's approach it from a more real-world direction: You have a bowl full of beans with some stones mixed in with them. What steps would you use to end up with a bowl full of beans but no stones?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

P.S. I changed the topic title to better reflect the question.
David Crane
Greenhorn

Joined: Aug 29, 2009
Posts: 15
yes, I do want a bowl of beans, how do I do that in code, I had thought the method you use was related to booleans, so I am grateful for the assitance
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

David Crane wrote:I had thought the method you use was related to booleans ...

Again, that mystifies me. Booleans represent the values true or false. Nothing to do with stripping characters.

how do I do that in code

No one's going to write it for you. Start with how would you do it not in code. Write down the steps. Then try to see if you can write the code to follow those steps.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Bear Bibeault wrote:Nothing to do with stripping characters.

Or phone numbers, which actually confused me even more.

(Okay, I get a lot of false phone numbers, but my dating life is none of your business.)

What kinds of characters are you trying to remove? Have you considered replacing those characters with an empty string? If you look at the java.lang.String API you might find some methods for doing just that.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18710
    
  40

David Crane wrote:yes, I do want a bowl of beans, how do I do that in code


I think that you mssed the point entirely. Bear is suggesting that you do it without a computer first. Understand the process. And then translate it to code.

If you understand the process (with a physical pile of beans), then you'll understand how to do that in code.


David Crane wrote: I had thought the method you use was related to booleans, so I am grateful for the assitance


Here is something that may help you... Besides conditions (as if using the "if", "for", or "while" keyword), which probably applies to all programs. Looking into Booleans is a red-herring. Don't waste any more time on it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Kanitkar
Ranch Hand

Joined: Aug 08, 2009
Posts: 72
David Crane wrote:I would give it a go, the problem is I don't know how

I have my string in a boolean form now



what I can't figure out is how to tell it to remove non-numeric characters



First of all.....

boolean can not OR will not OR like NEVER have values other than true or false.

Secondly, what do you mean by "I have my string in a boolean form"?


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this was originally posted in the Swing forum, so perhaps
phoneNumberAsBoolean = Boolean.parseBoolean(phoneNumberAsString);

is an attempt at verifying data, in which case
an InputVerifier, or
a JFormattedTextField, or
a DocumentFilter

might be worth considering
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stripping characters from a string