• Post Reply Bookmark Topic Watch Topic
  • New Topic

String replace  RSS feed

 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement to replace characters in a string with ZEROs. Please find the code below.



Here, I have option to set flag true or false. When flag is true the data output should be 000000 else 012356. The string variable data can have data of any length.

When I do as above, i am NEW DATA as only 0, but i should get 000000. Please help me if i am doing any thing wrong here.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue you are experiencing lies inside your call to the java.lang.String substring method. Look at the arguments your pass to substring() and see if you can find your problem.


Hunter
 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this scenario, the substring returns "012356" and replaceAll of that is giving 0. Not sure what error I am doing. Can you please help me out
 
Loic Beylot
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the javadoc (String, ReplaceAll):

"public String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement."

you should find another way, the replaceAll method doesn’t change every character separately ;)

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HM has worked it out. You will only understand it if you go through the execution with a pencil and paper, and work out what is being passed to the replaceAll method.
 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh..Thanks

Doing this made a trick.

testdata=testdata.replaceAll("[0-9]", "0");

Is this correct way to do? I have one more scenario where the number is having negative sign as -012356. In this case also, i require 000000.

This means i have to replaceAll once again like

testdata=testdata.replaceAll("-","");

Let me know if my understanding is correct
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you review Java regular expression, you can create an expression such that you only need one replaceAll() call.
http://download.oracle.com/javase/tutorial/essential/regex/

Hunter
 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I tried it. Using Regular expression is working fine.

I found one more scenario where my input string is like -12546-78956-85462 having length of 18

but my out put should be 000000000000000 should be 15. The -negative sign should not be replaced with ZERO.

I tried using replaceAll as below but it did not work

testdata=testdata.replaceAll("[0-9[^-]]", "0"); //still i get 18 length, that includes -ve sign.

Please help and let me know if there is any thing wrong in this expression.

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are only trying to replace the digits with "0" then the regex "[0-9]" should match all of the digits? This is what you originally had, did it not work?

Hunter
 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first scenario worked perfectly. Thanks for helping me on that.

I came across one more scenario, as mentioned below. Upon trying with below line of code, the output is not correct.

testdata=testdata.replaceAll("[0-9[^-]]", "0"); //still i get 18 length, that includes -ve sign.

Can someone please throw light on this. Appreciate your help in advance.

Eshwar

 
Loic Beylot
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your regular expression, you select every "-" and you change them by '0", but you shouldn’t do that.

In my humble opinion, you need two replaceAll.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!