• 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

Change \ into \\ in a string

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone have a idea to change a \ into a \\ in a string(buffer) ?
A pitfall is that if the string already contains \\ it should not be changed.
For instance String s = "c:\\install\test\test.xml"
The result should be s = "c:\\instal\\test\\test.xml"
Stefan
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java 2 Platform version 1.4 supports regular expressions:
Regular Expressions and the Java Programming Language
And looking at the class String documentation we find the method replaceAll(String regex, String replacement) available, which "replaces each substring of this string that matches the given regular expression with the given replacement."
I don't have the SDK version 1.4 installed, so I cannot use it.
Good Luck.
[ March 14, 2002: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ March 14, 2002: Message edited by: Steve Deadsea ]
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order for the code to work you have to escape the initial '\'s in the string:
String s = "c:\\\\install\\test\\test.xml"
System.out.println(s);
outputs: c:\\install\test\test.xml
...now run it through the code above and you'll get what you want.
String s = "c:\\install\test\test.xml"
System.out.println(s);
outputs: c:\install est est.xml
It reads the first new occurence of '\' as an escape character, thus it sees '\t' (tab) and not '\' 't' separately
[ March 14, 2002: Message edited by: David Duran ]
 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve,
the code works fine. Thx.
David, I don't understand why you write your comment.
As stated, I have no problem in using Steve's code (working with JDK1.3.1)
Stefan
reply
    Bookmark Topic Watch Topic
  • New Topic