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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 18745
    
  40

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: 41574
    
  54
Have you tried " replaceAll("'", "\"") " ?


Ping & DNS - my free Android networking tools app
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18745
    
  40

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: 18745
    
  40

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: 18745
    
  40




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!
 
 
subject: Replace character ' in a string