How to get the path from a file URL? - java

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:[/]*", "");

Related

ReplaceAll Method

I want to replace "\" with this "/" in my string.
I am using method replaceAll for this. But it is giving me error.
String filePath = "D:\pbx_u01\apache-tomcat-6.0.32\bin\uploadFiles\win.jpg";
String my_new_str = filePath.replaceAll("\\", "//");
Just use replace.
The method replaceAll takes a regular expression and yours would be malformed.
String filePath = "D:/pbx_u01/apache-tomcat-6.0.32/bin/uploadFiles/win.jpg";
System.out.println(filePath.replace("/", "\\"));
Output
D:\pbx_u01\apache-tomcat-6.0.32\bin\uploadFiles\win.jpg
When you absolutely want to use regex for this, use:
String filePath = "D:\\pbx_u01\\apache-tomcat-6.0.32\\bin\\uploadFiles\\win.jpg";
String my_new_str = filePath.replaceAll("\\\\", "/");
Output of my_new_str would be:
D:/pbx_u01/apache-tomcat-6.0.32/bin/uploadFiles/win.jpg
Just be sure to notice the double backslashes \\ in the source String (you used single ones \ in your question.)
But Mena showed in his answer a much simpler, more readable way to achive the same. (Just adopt the slashes and backslashes)
You are unable because character '//' should be typed only single '/'.
String filePath = "D:\\pbx_u01\\apache-tomcat-6.0.32\\bin\\uploadFiles\\win.jpg"
String my_new_str = filePath.replaceAll("\\", "/");
Above may be fail during execution giving you a PatternSyntaxException, because the first String is a regular expression so you use this,
String filePath = "D:\\pbx_u01\\apache-tomcat-6.0.32\\bin\\uploadFiles\\win.jpg"
String my_new_str = filePath.replaceAll("\\\\", "/");
Check this Demo ideOne

get a String between characters in java

I have a string like this
/data/data/com.example.MyClasses/files/السلام عليكم.pdf
I want to cut and extract the word السلام عليكم
by java code, how can I do this?
Thanks in advance.
String name = new File("/data/data/com.example.MyClasses/files/السلام عليكم.pdf").getName();
name = name.substring(0, name.lastIndexOf('.'));
Try substring and lastIndexOf method of String Class.
String str= "/data/data/com.example.MyClasses/files/السلام عليكم.pdf";
String result = str.substring(str.lastIndexOf("/")+1 , str.lastIndexOf("."));
You can parse the string with File like this:
import java.io.File;
File file = new File("/data/data/com.example.MyClasses/files/السلام عليكم.pdf");
String filename = FilenameUtils.removeExtension(file.getName());
Edit: even shorter
String filename = FilenameUtils.getBasename("/data/data/com.example.MyClasses/files/السلام عليكم.pdf");

Get substring image name

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()

Need to Trim Java String

I need help in trimming a string url.
Let's say the String is http://myurl.com/users/232222232/pageid
What i would like returned would be /232222232/pageid
Now the 'myurl.com' can change but the /users/ will always be the same.
I suggest you use substring and indexOf("/users/").
String url = "http://myurl.com/users/232222232/pageid";
String lastPart = url.substring(url.indexOf("/users/") + 6);
System.out.println(lastPart); // prints "/232222232/pageid"
A slightly more sophisticated variant would be to let the URL class parse the url for you:
URL url = new URL("http://myurl.com/users/232222232/pageid");
String lastPart = url.getPath().substring(6);
System.out.println(lastPart); // prints "/232222232/pageid"
And, a third approach, using regular expressions:
String url = "http://myurl.com/users/232222232/pageid";
String lastPart = url.replaceAll(".*/users", "");
System.out.println(lastPart); // prints "/232222232/pageid"
string.replaceAll(".*/users(/.*/.*)", "$1");
String rest = url.substring(url.indexOf("/users/") + 6);
You can use split(String regex,int limit) which will split the string around the pattern in regex at most limit times, so...
String url="http://myurl.com/users/232222232/pageid";
String[] parts=url.split("/users",1);
//parts={"http://myurl.com","/232222232/pageid"}
String rest=parts[1];
//rest="/232222232/pageid"
The limit is there to prevent strings like "http://myurl.com/users/232222232/users/pageid" giving answers like "/232222232".
You can use String.indexOf() and String.substring() in order to achieve this:
String pattern = "/users/";
String url = "http://myurl.com/users/232222232/pageid";
System.out.println(url.substring(url.indexOf(pattern)+pattern.length()-1);

Problem in replacing special characters in a String in java

I got a String as response from server which is like the below:
hsb:\\\10.217.111.33\javap\Coventry\
Now I want to parse this string in such a way that I need to replace all \ with /.
Also I need to remove the first part of the String which is hsb:\\\
So, my resultant string should be of like this :
10.217.111.33/javap/coventry/
Can anyone help me by providing sample java code for this problem.
Here you have a "dirty" startup "solution":
String s = "hsb:\\\\\\10.217.111.33\\javap\\Coventry\\";
String w = s.replace('\\', '/');
String x = w.replace("hsb:///", "");
String result = yourString.substring(7);
result = result.replaceAll("\\\\", "/");

Categories

Resources