Get substring image name - java

I have a string like myweb.com/blabla/blabla/image.jpg How could I get substring that starts at the end and ends when first "/" char? Expected string will be image.jpg.

Use Below Code for that.
String s="myweb.com/blabla/blabla/image.jpg";
int i=s.lastIndexOf("/");
s=s.substring(i+1);

String string = "myweb.com/blabla/blabla/image.jpg";
string.subString(string.lastIndexOf("/"));

Being String.substring clearly the solution for generic case, there is another way of doing your particular task in android:
String url = "myweb.com/blabla/blabla/image.jpg";
Uri uri = Uri.parse(url);
uri.getLastPathSegment();
getLastPathSegment in you case will return image.jpg. Apart from that you can easilly extract other information using uri object.

String name = fullpath.subString(fullpath.lastIndexOf("/")+1);
fullPath being the myweb.com/blabla/blabla/image.jpg

Use lastIndexOf method of String class..
String string = "myweb.com/blabla/blabla/image.jpg ";
String imageName = string.subString(string.lastIndexOf("/")+1);

Take a look at the File class. Has everything you need to get paths, filenames, extensions etc.
http://developer.android.com/reference/java/io/File.html
getName() will help.
http://developer.android.com/reference/java/io/File.html#getName()

Related

Replace multiple substrings within a string

I have string such as
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3"
I would like to replace the "substrings" "v1","v2" and "v3" with other values. How can I achieve this?
Does something like this work for your case?
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3";
String result = String.format(url.replaceAll("v[0-9]", "%s"), "arg1", "arg2", "arg3");
System.out.println(result); //www.test.com/blabla/?p1=arg1?p2=arg2?p3=arg3
Edit:
Just a brief explanation of what this does, it replaces all the v1,v2,v3,v4,v5,v6,v7,v8,v9,v0 in the original url for %s and then uses this in the format method so you can attribute what you want it to be.

Using regular expressions to rename a string

In java, I want to rename a String so it always ends with ".mp4"
Suppose we have an encoded link, looking as follows:
String link = www.somehost.com/linkthatIneed.mp4?e=13974etc...
So, how do I rename the link String so it always ends with ".mp4"?
link = www.somehost.com/linkthatIneed.mp4 <--- that's what I need the final String to be.
Just get the string until the .mp4 part using the following regex:
^(.*\.mp4)
and the first captured group is what you want.
Demo: http://regex101.com/r/zQ6tO5
Another way to do this would be to split the string with ".mp4" as a split char and then add it again :)
Something like :
String splitChar = ".mp4";
String link = "www.somehost.com/linkthatIneed.mp4?e=13974etcrezkhjk"
String finalStr = link.split(splitChar)[0] + splitChar;
easy to do ^^
PS: I prefer to pass by regex but it ask for more knowledge about regex ^^
Well you can also do this:
Match the string with the below regex
\?.*
and replace it with empty string.
Demo: http://regex101.com/r/iV1cZ8
Try below code,
private String trimStringAfterOccurance(String link, String occuranceString) {
Integer occuranceIndex = link.indexOf(occuranceString);
String trimmedString = (String) link.subSequence(0, occuranceIndex + occuranceString.length() );
System.out.println(trimmedString);
return trimmedString;
}

How to extract a url from a string in Java?

I have a string containing a short-code which looks like the one below:
some text...
[video url="http://www.example.com/path/to/my/video.ext"]
...some more text...
I want to be able to first check if the string contains that short-code and second extract the URL from it in Java (specifically Android).
use this regex for checking and grabbing url:
\[\w+\s+url="(?<urllink>)[^"]*"\s*]
and get gorup named urllink
try as:
String str = "[video url=\"http://www.example.com/path/to/my/video.ext\"]";
if (str.contains("url=\""))
{
int indexoff = str.indexOf("url=\"");
int indexofff = str.indexOf("\"]");
String strurl = str.substring(indexoff, indexofff - indexoff);
strurl = strurl.Replace("url=\"", ""); //get url string here
}
Android provides several function for this purpose. SOme of this are:
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
http://developer.android.com/reference/org/apache/http/client/utils/URLEncodedUtils.html
String url = "whatever"
Boolean myBool = url.contains("ate");
String contains. Not sure what extract url means, but the string class has lots of useful functions.
A powerful and maintainable manner it to Java URL.class, then, you can mix with Regex

How to get the path from a file URL?

I have Strings in this format :
file://c:/Users/....
file://E:/Windows/....
file:///f:/temp/....
file:///H:/something/....
How can I get just c:/Users/... or H:/something/... ?
Tested and will replace an arbitrary number of slashes.
String path = yourString.replaceFirst("file:/*", "");
And if you only want it to match two or three slashes
String path = yourString.replaceFirst("file:/{2,3}", "");
String path = new java.net.URI(fileUrl).getPath();
you can replace the string "file://" in your string with nothing:
String path = yourString.replace("file://", "");
What about that?
String path = yourString.replaceFirst("file:[/]*", "");

Java: getting parameters from a URI who contains a file

let's say I have a file located in:
http://example.com/123.app
now I get the file name using the following (u is an entire url string):
String fileName = u.substring( u.lastIndexOf('/')+1, u.length() );
but I want to put on the same file name also parameters, so it'll look like, this:
http://example.com/123.app?id=87983
And I want to have a String fileName which will contain '123.app', and also String id which will contain '87983' and possibly more parameters.
How would I go about achieving this?
Firstly, take a look at this post, which uses the URL class to make working with the different parts of the URL string a lot easier.
Could you share a link to an URL parsing implementation?
Secondly, you would need to take the Query part of the URL and the Path part of the URL and substring the returned values to get the information that you desire. It should be pretty straight forward.
Use the API of URI! That's what it's for. Forget all this substring/regex/spit stuff.
you need to use the split method on string. So for example on your fileName string
String[] mystrings = fileName.split("?");
then mystrings[0] is your filename and mystrings[1] is your parameter
A simple way is : just repeat substring :
int qidx = filename.indexOf("?");
String realFilename = filename.substring(0, qidx);
String parameters = filename.substring(qidx+1);
and so on for parsing parameters.
If you are writing a servlet try :
String fileName = request.getServletPath();
and for the parameters somthing like
String id = request.getParameter("id");
Try this regex:
String s = "http://example.com/123.app?id=87983";
String[] split = s.split(".*/|\\?id=");
String filename = split[1];
String id = (split.length == 3) ? split[2] : null;

Categories

Resources