Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing substrings in a string

 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!
I wanted to make a program part, which replaces "<" and ">" with "<" and ">" so it won't dissappear in HTML context. My problem is that the following method doesn't do it at all:



Why isn't it working?
Thanks!
 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String Objects are immutable -- meaning they can't be changed.


str.replaceAll("<","<");
str.replaceAll(">","&ht;");


The replaceAll() method simply returns a new string object with the changes requested. Since you are not assigning the return value, you are simply discarding the String with the changes.

Henry
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should i use str = str.replaceAll() then?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Csaba Kassitzky:
Should i use str = str.replaceAll() then?


Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic