• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stripping characters from a string

 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nothing Swing-specific to do with the problem,
but a regular expression would do the job
 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you give me an example of the code I would use to do this, Michael?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. I changed the topic title to better reflect the question.
 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21415
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic