I have a query a method in which the parameter is coming as a file name that upon debugging I have analyzed, as shown below:
private processfile ( string filePath)
{
}
Now this file path can be like:
C:\abc\file1.txt
or
C:\abc\def\file1.txt
or
C:\ghj\ytr\wer\file1.txt
Now my query is that I have to extract the file name only and have to store in a string parameter. So I have to store the file1.txt in a string, let say in a string parameter s , so finally s will be stored as
String s = file1.txt
How to achieve this?
This should do the trick
String s = new File(filepath).getName()
although I would rename filepath to filePath.
You can find File#getName() documentation here
You can use indexOf and substring for this case:
String s = filepath.substring(filepath.lastIndexOf(File.separator)+1);
File.getName also takes similar approach, see source below:
public String getName() {
int index = path.lastIndexOf(separatorChar);
if (index < prefixLength) return path.substring(prefixLength);
return path.substring(index + 1);
}
Related
I have a string like this:
"core/pages/viewemployee.jsff"
From this code, I need to get "viewemployee". How do I get this using Java?
Suppose that you have that string saved in a variable named myString.
String myString = "core/pages/viewemployee.jsff";
String newString = myString.substring(myString.lastIndexOf("/")+1, myString.indexOf("."));
But you need to make the same control before doing substring in this one, because if there aren't those characters you will get a "-1" from lastIndexOf(), or indexOf(), and it will break your substring invocation.
I suggest looking for the Javadoc documentation.
You can solve this with regex (given you only need a group of word characters between the last "/" and "."):
String str="core/pages/viewemployee.jsff";
str=str.replaceFirst(".*/(\\w+).*","$1");
System.out.println(str); //prints viewemployee
You can split the string first with "/" so that you can have each folder and the file name got separated. For this example, you will have "core", "pages" and "viewemployee.jsff". I assume you need the file name without the extension, so just apply same split action with "." seperator to the last token. You will have filename without extension.
String myStr = "core/pages/viewemployee.bak.jsff";
String[] tokens = myStr.split("/");
String[] fileNameTokens = tokens[tokens.length - 1].split("\\.");
String fileNameStr = "";
for(int i = 0; i < fileNameTokens.length - 1; i++) {
fileNameStr += fileNameTokens[i] + ".";
}
fileNameStr = fileNameStr.substring(0, fileNameStr.length() - 1);
System.out.print(fileNameStr) //--> "viewemployee.bak"
These are file paths. Consider using File.getName(), especially if you already have the File object:
File file = new File("core/pages/viewemployee.jsff");
String name = file.getName(); // --> "viewemployee.jsff"
And to remove the extension:
String res = name.split("\\.[^\\.]*$")[0]; // --> "viewemployee"
With this we can handle strings like "../viewemployee.2.jsff".
The regex matches the last dot, zero or more non-dots, and the end of the string. Then String.split() treats these as a delimiter, and ignores them. The array will always have one element, unless the original string is ..
The below will get you viewemployee.jsff:
int idx = fileName.replaceAll("\\", "/").lastIndexOf("/");
String fileNameWithExtn = idx >= 0 ? fileName.substring(idx + 1) : fileName;
To remove the file Extension and get only viewemployee, similarly:
idx = fileNameWithExtn.lastIndexOf(".");
String filename = idx >= 0 ? fileNameWithExtn.substring(0,idx) : fileNameWithExtn;
I'm trying to convert a file into a String and after that i will replace the name of the converted file without non numeric characters but when i replace it the file extension of the file is also replaced. for example (2014.05-06.txt -> 20140506.txt but whats happening is 20140506txt) i want to remain the .txt, .log or any type of extension.
String strDatefiles = Arrays.toString(saDateFiles).replaceAll("[\\W]", "");
Edited:
String[] saDateFiles = fileList.list();
String strDatefiles = Arrays.toString(saDateFiles.substring(0, saDateFiles.lastIndexOf("."))).replaceAll("[\\W]", "");
this saDateFiles.lastIndexOf("."))) have error replace with a length?
Edited2:
String[] saDateFiles = fileList.list();
String strDatefiles = Arrays.toString(saDateFiles).substring(0, Arrays.toString(saDateFiles).lastIndexOf(".")).replaceAll("[\\W]","");
System.out.println(strDatefiles);`
Output: 20140502txt20140904 (I have 2 files inside)
I would take the indexOf the last . in the String, and then manipulate the two substrings. For example,
String saDateFiles = "2014.05-06.txt";
int lastDot = saDateFiles.lastIndexOf('.');
String strDatefiles = saDateFiles.substring(0, lastDot).replaceAll("\\D", "")
.concat(saDateFiles.substring(lastDot));
System.out.println(strDatefiles);
Outputs (as requested)
20140506.txt
As you noticed, the above was for one file name. To do it for an array of file names, you could use a for-each loop and the above code like
String[] saDateFilesArr = fileList.list();
for (String saDateFiles : saDateFilesArr) {
int lastDot = saDateFiles.lastIndexOf('.');
String strDatefiles = saDateFiles.substring(0, lastDot)
.replaceAll("\\D", "").concat(saDateFiles.substring(lastDot));
System.out.println(strDatefiles);
}
Apply your replace function to the part of file name before the ".". You can extract this part with the code :
fileName.substring(0, fileName.lastIndexOf(".")) ;
Use :
String strDatefiles = Arrays.toString(saDateFiles.substring(0, saDateFiles.lastIndexOf("."))).replaceAll("[\\W]", "");
I am still a novice with regular expressions, "regex", etc... in Java.
If I have an url like this : "http://somedomain.someextention/somefolder/.../someotherfolder/somepage"
What is the simplest way to get :
"somedomain.someextention" ?
"somefolder/.../someotherfolder/somepage" ?
"somepage" ?
Thanks !
You don't have to (and probably shouldn't) use regex here. Instead use classes defined to handle things like this. You can use for example URL, URI, File classes like
String address = "http://somedomain.someextention/somefolder/.../someotherfolder/somepage";
URL url = new URL(address);
File file = new File(url.getPath());
System.out.println(url.getHost());
System.out.println(url.getPath());
System.out.println(file.getName());
Outpit:
somedomain.someextention
/somefolder/.../someotherfolder/somepage
somepage
Now you can need to get rid of / at start of path to your resource. You can use substring(1) here if resource starts with /.
But if you really must use regex you can try with
^https?://([^/]+)/(.*/([^/]+))$
Now
group 1 will contain host name,
group 2 will contain path to resource
group 3 will contain name of resource
The best way to get those components is to use the URI class; e.g.
URI uri = new URI(str);
String domain = uri.getHost();
String path = uri.getPath();
int pos = path.lastIndex("/");
...
// or use File to parse the path string.
You could do it using regexes on the raw url string, but there is a risk that you won't correctly cope with all of the variability that is possible in a URL. (Hint: the regex supplied by #Pchenko doesn't :-)) And you would definitely need to use a decoder to deal with possible percent encoding.
This is not a regexp or URI use but simple substring code as an excersise material. Missing few corner case format validation.
int lastDelim = str.lastIndexOf('/);
if (lastDelim<0) throw new IllegalArgumentException("Invalid url");
int startIdx = str.indexOf("//");
startIdx = startIdx<0 ? 0 : startIdx+2;
int pathDelim = str.indexOf('/', startIdx);
String domain = str.substring(startIdx, pathDelim);
String path = str.substring(pathDelim+1, lastDelim);
String page = str.substring(lastDelim+1);
If you would like to use regex to decode the URL instead of using the URI class, as described in the previous answers, the below link gives a nice tutorial of regex, and it explains decoding a sample URL as well. You could learn it there and try it out.
http://www.beedub.com/book/2nd/regexp.doc.html
It's not regex, or scalable at that, it works though:
public class SomeClass
{
public static void main(String[] args)
{
SomeClass sclass = new SomeClass();
String[] string =
sclass.parseURL("http://somedomain.someextention/somefolder/.../someotherfolder/somepage");
System.out.println(string[0]);
System.out.println(string[1]);
System.out.println(string[2]);
}
private String[] parseURL(String url)
{
String part1 = url.substring("http://".length(), url.indexOf("/", "http://".length()));
String part2 = url.substring("http://".length() + part1.length() + 1, url.lastIndexOf("/"));
String part3 = url = url.substring(url.lastIndexOf("/") + 1);
return new String[] { part1, part2, part3 };
}
}
Output:
somedomain.someextention
somefolder/.../someotherfolder
somepage
I am trying to get the extension (dk, com, org, eu) or any other domain extension from a String.
for example:
http://www.example.com/siteone/sitetwo/currentpage
From this String i would like to get the .com
I could go the very messy way around and do subString however the problem comes when an url looks like this:
dk.webpage.otherstuff.com/page
So how will i go around this in a way that doesnt require me to check everything every step of the way
Use the getHost() method like this:
public static String getDomainName(String testUrl) throws URISyntaxException {
URI fullUri = new URI(testUrl);
String domainName = fullUri.getHost();
return domainName.startsWith("www.") ? domainName.substring(4) : domainName;
}
After you have done that then just use subString for the .com part of your domain name.
Use Guava's InternetDomainName class. Specifically have a look at the publicSuffix method.
Try this:
String ext = url.replaceAll(".*//[^/]*(\\.\\w+)/.*", "$1");
Some test code:
String url = "http://www.example.com/siteone/sitetwo/currentpage";
String ext = url.replaceAll(".*//[^/]*(\\.\\w+)/.*", "$1");
System.out.println(ext);
Output:
.com
Try this :
private String getExtensionFromDomain(String domainName){
int p = domainName.lastIndexOf(".") +1;
return domainName.substring(p);
}
In case of example.co.ma this will output : .ma
I have as input a string that is a URI. how is it possible to get the last path segment (that in my case is an id)?
This is my input URL:
String uri = "http://base_path/some_segment/id"
and I have to obtain the id I have tried with this:
String strId = "http://base_path/some_segment/id";
strId = strId.replace(path);
strId = strId.replaceAll("/", "");
Integer id = new Integer(strId);
return id.intValue();
but it doesn't work, and surely there must be a better way to do it.
is that what you are looking for:
URI uri = new URI("http://example.com/foo/bar/42?param=true");
String path = uri.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
int id = Integer.parseInt(idStr);
alternatively
URI uri = new URI("http://example.com/foo/bar/42?param=true");
String[] segments = uri.getPath().split("/");
String idStr = segments[segments.length-1];
int id = Integer.parseInt(idStr);
import android.net.Uri;
Uri uri = Uri.parse("http://example.com/foo/bar/42?param=true");
String token = uri.getLastPathSegment();
Here's a short method to do it:
public static String getLastBitFromUrl(final String url){
// return url.replaceFirst("[^?]*/(.*?)(?:\\?.*)","$1);" <-- incorrect
return url.replaceFirst(".*/([^/?]+).*", "$1");
}
Test Code:
public static void main(final String[] args){
System.out.println(getLastBitFromUrl(
"http://example.com/foo/bar/42?param=true"));
System.out.println(getLastBitFromUrl("http://example.com/foo"));
System.out.println(getLastBitFromUrl("http://example.com/bar/"));
}
Output:
42
foo
bar
Explanation:
.*/ // find anything up to the last / character
([^/?]+) // find (and capture) all following characters up to the next / or ?
// the + makes sure that at least 1 character is matched
.* // find all following characters
$1 // this variable references the saved second group from above
// I.e. the entire string is replaces with just the portion
// captured by the parentheses above
I know this is old, but the solutions here seem rather verbose. Just an easily readable one-liner if you have a URL or URI:
String filename = new File(url.getPath()).getName();
Or if you have a String:
String filename = new File(new URL(url).getPath()).getName();
If you are using Java 8 and you want the last segment in a file path you can do.
Path path = Paths.get("example/path/to/file");
String lastSegment = path.getFileName().toString();
If you have a url such as http://base_path/some_segment/id you can do.
final Path urlPath = Paths.get("http://base_path/some_segment/id");
final Path lastSegment = urlPath.getName(urlPath.getNameCount() - 1);
In Android
Android has a built in class for managing URIs.
Uri uri = Uri.parse("http://base_path/some_segment/id");
String lastPathSegment = uri.getLastPathSegment()
If you have commons-io included in your project, you can do it without creating unecessary objects with org.apache.commons.io.FilenameUtils
String uri = "http://base_path/some_segment/id";
String fileName = FilenameUtils.getName(uri);
System.out.println(fileName);
Will give you the last part of the path, which is the id
In Java 7+ a few of the previous answers can be combined to allow retrieval of any path segment from a URI, rather than just the last segment. We can convert the URI to a java.nio.file.Path object, to take advantage of its getName(int) method.
Unfortunately, the static factory Paths.get(uri) is not built to handle the http scheme, so we first need to separate the scheme from the URI's path.
URI uri = URI.create("http://base_path/some_segment/id");
Path path = Paths.get(uri.getPath());
String last = path.getFileName().toString();
String secondToLast = path.getName(path.getNameCount() - 2).toString();
To get the last segment in one line of code, simply nest the lines above.
Paths.get(URI.create("http://base_path/some_segment/id").getPath()).getFileName().toString()
To get the second-to-last segment while avoiding index numbers and the potential for off-by-one errors, use the getParent() method.
String secondToLast = path.getParent().getFileName().toString();
Note the getParent() method can be called repeatedly to retrieve segments in reverse order. In this example, the path only contains two segments, otherwise calling getParent().getParent() would retrieve the third-to-last segment.
You can also use replaceAll:
String uri = "http://base_path/some_segment/id"
String lastSegment = uri.replaceAll(".*/", "")
System.out.println(lastSegment);
result:
id
You can use getPathSegments() function. (Android Documentation)
Consider your example URI:
String uri = "http://base_path/some_segment/id"
You can get the last segment using:
List<String> pathSegments = uri.getPathSegments();
String lastSegment = pathSegments.get(pathSegments.size() - 1);
lastSegment will be id.
I'm using the following in a utility class:
public static String lastNUriPathPartsOf(final String uri, final int n, final String... ellipsis)
throws URISyntaxException {
return lastNUriPathPartsOf(new URI(uri), n, ellipsis);
}
public static String lastNUriPathPartsOf(final URI uri, final int n, final String... ellipsis) {
return uri.toString().contains("/")
? (ellipsis.length == 0 ? "..." : ellipsis[0])
+ uri.toString().substring(StringUtils.lastOrdinalIndexOf(uri.toString(), "/", n))
: uri.toString();
}
you can get list of path segments from the Uri class
String id = Uri.tryParse("http://base_path/some_segment/id")?.pathSegments.last ?? "InValid URL";
It will return id if the url is valid, if it is invalid it returns "Invalid url"
Get URL from URI and use getFile() if you are not ready to use substring way of extracting file.