• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.replace() not work on WinXP?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic