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

Strings

R Dom
Greenhorn

Joined: Sep 08, 2010
Posts: 19
I am processing some strings like 005a ,006b , 001d

I want to check if there is any character present a to z in above string and if the character is > b then i will replace is by b.
001d will be replaced by 001b

I want to make a generalize code and i dont want to hardcode anything for comparison.
Could you please suggest me what i can do to achieve this.

Thanks


Cheers,
RD
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi..

One can loop through the string by converting is to a character array. One can check the ASCII value of the character, if ASCII doesn't satisfy our requirement we can change that character to the desired char(b).

For this a method can be created which will have the string(need to be modified) and the highest alphabet (ASCII or char) as parameter.

Regards,
Sudipta Kumar Laha
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
What about the replaceXXX methods of String?

And please be more specific in your thread titles.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Campbell has just mentioned it - I was about to type the code but nonetheless, here's a simple example

I am assuming that your strings follow the format as you have specified which means 3 digits followed by a character which may be anything between a-z

The following code will work for you


What the first argument to replaceAll does is that it specifies a regular expression. Now the regular expression simply replaces any character except 0-9 or a or b by b. Since you are strings are composed of all digits and one character, this will replace all characters except a or b by b.

For more info on regular expressions check the api for java.util.regex.Pattern and there is also another method replaceFirst. You may use them as required - check the java.lang.String api for more info on these functions.

Cheers


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Soniya Ahuja, if you look on the "Beginning Java" forum, you will read this:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Please don't give so much code out. It does nobody any good to be spoon-fed; everybody will remember it a lot better if they have looked it up for themselves.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Alright Campbell, sorry I didn't read that part, I guess I should have read it first. At a few places I have put down some code examples. I'll keep them, but I'll make sure that the next time I post, I make sure that I am not divulging too much
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Thank you, Soniya Ahuja
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Campbell Ritchie wrote:Thank you, Soniya Ahuja

Do you really need to thank me
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Soniya Ahuja wrote: . . . Do you really need to thank me
Yes.
 
 
subject: Strings