This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
ashok ks
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you're using single quotes
 
roy joseph
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
opps i missed this.
do this.
String s1 = "D:\\xml\\haibye.xml";
 
ashok ks
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i convert D:\xml\haibye.xml to D:\\xml\\haibye.xml ?
 
roy joseph
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic