Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Replacing substrings in a string  RSS feed

 
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
Sheriff
Posts: 23260
124
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
Sheriff
Posts: 24217
38
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
Boost this thread!