File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replace character ' in a string

 
Tomita Militaru
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Doesn't seem to work, searched the forum for other replace issues, but there are houndreds of results.
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tomita Militaru
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried:



Still no result.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried " replaceAll("'", "\"") " ?
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomita Militaru wrote:
Still no result.


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

Henry
 
Tomita Militaru
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn that was so obviously!

Thanks Henry!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic