Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Filename using substring

 
nandhini sruthi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need help for get only the filename from attachment url D:\MDM\get_url.txt it is not default.this url is change everytime attachment.please help me how to get filename without txt.

thanks in advance.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds more like a beginner's question.
You can parse the String with the methods of the String class and find the index of the "." and then get a substring.
[ July 23, 2008: Message edited by: Campbell Ritchie ]
 
nandhini sruthi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried using substring.but I got some problem in that. Please give the solution. I put my code here.



If i use the like above(ie.I pass the value directly)i got exact filename get_url.but i can't use like this.When I click the browse button I take any file and its path may be different. I definitely use like follows. But that time I can't get exact filename.


If i use the like above I got filename D:\MDM\get_url.please give the solution.

Thanks in advance.
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check for file path seperator. In your case, it seems to be the '\' and not the '/' which you are checking.
1. Check what the file path seperator is and store it in a variable.
2. Use this variable to get to the last occurence of the seperator.
 
vedha vas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nandhini Sruthi ,

Try this way,

String url = "c\\documentsandsettings\\user\\privatefiles\\get_url.txt";
int dotCnt = url.indexOf(".");
String str = url.substring(0, dotCnt);
int slashCnt = str.lastIndexOf("\\");
String filename = str.substring(slashCnt+1);
System.out.println(filename);
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niki Nono was nearly correct; you look for the path separator and file separator. But you don't have to search far for them; there are fields in the File class called separator or pathSeparator which encapsulate that information, so you can find the last index of those characters. You can also find them from the System class.
Look through the File class and see which you need; I think it's "separator," not "path separator."
No need to hard-code "\\" or "/" which would limit portability.

Try

. . . myURL.substring(myURL.lastIndexOf(File.separator) + 1, myURL.lastIndexOf(".")); or something similar.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My technique will not cause problems if you pass a String like "myFile.txt" without / or \\ but you will get problems if there is no "." in it anywhere. That might require two substring calls.
 
nandhini sruthi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for reply.I got the answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic