I have the String content://com.android.contact/data/5032
in a variable Str1
. I want to manipulate Str1
so that I will get 5032
in another string variable.
Can anyone suggest the answer?
I have the String content://com.android.contact/data/5032
in a variable Str1
. I want to manipulate Str1
so that I will get 5032
in another string variable.
Can anyone suggest the answer?
String str1 = "content://com.android.contact/data/5032"
String val = str1.substring(str1.lastIndexOf("//")+1);
If you want go get digits from the given string try this:
String str = "content://com.android.contact/data/5032";
String str2 = str.replaceAll("\\D+","");
System.out.println(str2);
Output:
5032
If you want to split try this:
String[] string = str.split("//|/");
System.out.println(string[string.length -1 ]);
Output:
5032
String str1 = "content://com.android.contact/data/5032";
String str2 = str1.substring(str1.lastIndexOf("/")+1, str1.length());
if you only want the last 4 chars, you can do something like this
String s = "this is a string";
String ss = s.substring(s.length()-4, s.length());
but if you need to extract the number from random positions, you will have to use regular expressions.
Please write names of variables starting with a small letter. You can use the split
method to do this. You might find this related question interesting: How to split a string in Java.