get a String between characters in java - 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");

Related

How to get selected String from Text

I try to get only this part "10.135.57.1/24" in "10.135.57.1/24 05492518979" in android. How can I do this?
I tried below to use substring but it can use for get integer How can I get only 10.135.57.1/24 ?
For this string the following approach will work:
String[] parts = "10.135.57.1/24 05492518979".split(" ");
String partThatYouNeed = parts[0];
In the substring method you're defining which subsection of the String you're after.
If you did the following you'd get your result:
String whatYouWant = "10.135.57.1/24 05492518979".substring(0, 14);
Or using substring
String yourString = "10.135.57.1/24 05492518979";
int index = yourString.indexof(" ");
String partThatYouNeed = yourString.substring(0,index);
How about this :-)
"10.135.57.1/24 05492518979".replaceFirst(" \\d+$", "")
or this:
"10.135.57.1/24 05492518979".replaceFirst(" .+$", "")

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;
}

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

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

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