aspose file tools
The moose likes Beginning Java and the fly likes Replace character ' in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Replace character Watch "Replace character New topic
Author

Replace character ' in a string

Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37


Doesn't seem to work, searched the forum for other replace issues, but there are houndreds of results.


Poor is the man whose pleasures depend on the permission of another.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16696
    
  19

Tomita Militaru wrote:

Doesn't seem to work, searched the forum for other replace issues, but there are houndreds of results.



With the replaceAll() method, the first parameter is a regex string, and the second parameter is a regex replacement string. With both strings, the backslash has special meaning and needs to be escaped.

Another option is to use the replace() method instead -- which doesn't use regexes.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
Tried:



Still no result.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35258
    
    7
Have you tried " replaceAll("'", "\"") " ?


Android appsImageJ pluginsJava web charts
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16696
    
  19

Tomita Militaru wrote:
Still no result.


http://faq.javaranch.com/java/TellTheDetails

Henry
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
Well nothing happens, the string isn't changed at all. My input is this string: "=C3+'C\"3+C3'" and I want to replace the ' with "
No exceptions. I have an array with that kind of strings and I tried the replace/replaceall method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16696
    
  19

Show us some code please -- it would help us if you tell us how you are calling the method. For all we know, you could be doing something really silly like....

str.replace('\'', '\"');

Henry
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16696
    
  19




Strings are immutable. String methods like replace(), replaceAll(), and even substring(), do not change the string value. They return a new string.

Of course, you know this, since you used substring() correctly.

Henry
Tomita Militaru
Ranch Hand

Joined: Jan 16, 2009
Posts: 37
Damn that was so obviously!

Thanks Henry!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replace character ' in a string
 
Similar Threads
how to delete the map elements when iterating the map
Simple Eclipse Question
Include DTD in DTD
Program help
bitwise & operation on char variable