• Post Reply Bookmark Topic Watch Topic
  • New Topic

String.replace() not work on WinXP?  RSS feed

 
Anthony Yip
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I have a program running fine on Windows 98 but not with WinXP. It encountes a NoSuchMethodError: java.lang.String.replace() ....

Then, I tried a very simple example as follow:



Similar error found.

Could anyone know the reason?

Actually, I am trying to trim all the space from a string. If I don't use replace(), what is the best alternative?

Regards
Anthony Yip
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.replace(String, String) was introduced in JDK 1.4; your XP machine apparently is running an older version of Java and needs an upgrade.

There was no handy way to accomplish what you're doing in earlier JDKs; the best way is just to loop over the characters and accumulate the non-spaces in a StringBuffer, then call toString() at the end.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no replace(String string, String otherString) method in the String class. There is a replace(char oldChar, char newChar). In Java you surround a String literal with "" and a character primitive with ''. (I think Ernest is thinking of the replaceFirst(String regex, String replacement) or replaceAll(String regex, String replacement) methods)

However, if you are trying to trim whitespace from the start and end of a String, I suggest you consider the trim() method of String.
[ June 07, 2004: Message edited by: Paul Sturrock ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!