• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
R Dom
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sudipta Laha
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the replaceXXX methods of String?

And please be more specific in your thread titles.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Soniya Ahuja
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Thank you, Soniya Ahuja

Do you really need to thank me
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soniya Ahuja wrote: . . . Do you really need to thank me
Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic