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?

1
  • i am trying split function with "/" but yet to get desired result. Commented Feb 4, 2013 at 10:38

5 Answers 5

3
String str1 = "content://com.android.contact/data/5032"
String val = str1.substring(str1.lastIndexOf("//")+1);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1
String str1 = "content://com.android.contact/data/5032";
String str2 = str1.substring(str1.lastIndexOf("/")+1, str1.length());

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.