GeeCON Prague 2014*
The moose likes Java in General and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "String" Watch "String" New topic
Author

String

ashok ks
Ranch Hand

Joined: Oct 31, 2003
Posts: 55
my dynamic input is D:\xml\haibye.xml , but i have to pass only
haibye.xml as a parameter to the parser. So i have to remove D:\xml\
When i compile the below mentione code, it gives the following error :
Illegal escape character String s1 = "D:\xml\haibye.xml";
^ ^

Code :
public class str {
public static void main(String args[]) {
String s1 = "D:\xml\haibye.xml";
String s = s1.replace('\\','/');
int p1 = s.lastIndexOf("/");
System.out.println("p1.."+p1);
int p2 = s.length();
System.out.println("p2..."+p2);
String res = s.substring(p1+1,p2);
System.out.println("res..."+res);
}
}
Any suggestion ?
roy joseph
Greenhorn

Joined: Dec 26, 2003
Posts: 25

you're using single quotes


be annoying .. Aku Soku Zan
roy joseph
Greenhorn

Joined: Dec 26, 2003
Posts: 25
opps i missed this.
do this.
String s1 = "D:\\xml\\haibye.xml";
ashok ks
Ranch Hand

Joined: Oct 31, 2003
Posts: 55
how can i convert D:\xml\haibye.xml to D:\\xml\\haibye.xml ?
roy joseph
Greenhorn

Joined: Dec 26, 2003
Posts: 25
huh?
you dont need to convert anything. since you are using a '\' inside a string you need to add an extra '\'.
so instead of something like this:
String s = "c:\dog";
do it like this:
String s = "c:\\dog";
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Better yet, don't use platform-specific '\' versus '/' characters... Just use '/' always (for directory separators), regardless of platform - unless you're passing the strings external to the app, such as when invoking Runtime.exec - then you have to use the proper platform-specific separators.
[ February 10, 2004: Message edited by: Billybob Marshall ]
 
 
subject: String