| Author |
Replacing substrings in a string
|
Csaba Kassitzky
Ranch Hand
Joined: Nov 29, 2005
Posts: 38
|
|
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
Joined: Sep 28, 2004
Posts: 16681
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Csaba Kassitzky
Ranch Hand
Joined: Nov 29, 2005
Posts: 38
|
|
|
Should i use str = str.replaceAll() then?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24043
|
|
Originally posted by Csaba Kassitzky: Should i use str = str.replaceAll() then?
Yes.
|
[Jess in Action][AskingGoodQuestions]
|
 |
 |
|
|
subject: Replacing substrings in a string
|
|
|