aspose file tools*
The moose likes Beginning Java and the fly likes Replacing substrings in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replacing substrings in a string" Watch "Replacing substrings in a string" New topic
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: 18120
    
  39

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: 24166
    
  30

Originally posted by Csaba Kassitzky:
Should i use str = str.replaceAll() then?


Yes.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replacing substrings in a string
 
Similar Threads
a simple question
Replacing substrings in string
Transformation Problem For Arabic/French Character : HTML to XML
Regex for adding CDATA to XML nodes
String replaceAll()