• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in string replacement

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
https://coderanch.com/t/578980/java/java/strings
which has many useful references.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic