• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in string replacement  RSS feed

 
Priyatham Anisetty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a big string which looks " testclasses.abc.System_A" . I was trying to replace '.' with '/' .

I tried using

It is not working. Any suggestions please .
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Java Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the code your are giving us, the tmp will not be replaced by the String that you want. This happens because when you invoke the replace method, a new String reference is created, which you must set yout tmp to. Meaning you have to write tmp = tmp.replace('.','/');
In case you have difficulty understanding how this whole thing with the String work, try reading this thread
http://www.coderanch.com/t/578980/java/java/strings
which has many useful references.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read the docs for the replace() method, you'll see that it states that it returns the new String. You'll also see that it does not say anything about modifying the existing String. (And, though you may not have learned this yet, it couldn't modify the existing String even if it wanted to, since Strings are immutable.)

It's always worth studying the documentation carefully when something doesn't behave according to your assumptions.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!