0

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?

Xavi López
  • 27,550
  • 11
  • 97
  • 161
Mayur Shah
  • 1,009
  • 11
  • 16

5 Answers5

3
String str1 = "content://com.android.contact/data/5032"
String val = str1.substring(str1.lastIndexOf("//")+1);
Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
2

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
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
1
String str1 = "content://com.android.contact/data/5032";
String str2 = str1.substring(str1.lastIndexOf("/")+1, str1.length());
Sandeep
  • 1,154
  • 10
  • 16
0

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.

Cristian Holdunu
  • 1,880
  • 1
  • 18
  • 43
0

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.

Community
  • 1
  • 1
TN888
  • 7,659
  • 9
  • 48
  • 84