0

I have a string which has the values like this (format is the same):

name:xxx
occupation:yyy
phone:zzz

I want to convert this into an array and get the occupation value using indexes.

Any suggestions?

7 Answers 7

3

Basically you would use Java's split() function:

String str = "Name:Glenn Occupation:Code_Monkey";

String[] temp = str.split(" ");
String[] name = temp[0].split(":");
String[] occupation = temp[1].split(":");

The resultant values would be:

name[0] - Name
name[1] - Glenn

occupation[0] - Occupation
occupation[1] - Code_Monkey
Sign up to request clarification or add additional context in comments.

4 Comments

What in case Name:Glenn Nelson(inclusive of spaces)
Well that would change the playing field. He asked for it in the format of xxx:xxx yyy:yyy not xxx:xx x yyy:y yy.
am not declaring the String statically, am storing it dynamically, the values comes like one below the other (like Name:Glenn and then in the new line Occupation:Code_Monkey)...??????????
I'm just giving you the underlying principal of what you were asking for. Doing it dynamically depends on your program's IO.
1

Read about Split functnio. You can split your text by " " and then by ":"

Comments

0

I would recommend using String's split function.

1 Comment

The values comes dynamically, iam storing in a String my question is how to convert that String values into an array and get the value?
0

Sounds like you want to convert to a property Map rather than an array.

e.g.

String text = "name:xxx occupation:yyy phone:zzz";
Map<String, String> properties = new LinkedHashMap<String, String>();
for(String keyValue: text.trim().split(" +")) {
   String[] parts = keyValue.split(":", 2);
   properties.put(parts[0], parts[1]);
}
String name = properties.get("name"); // equals xxx

This approach allows your values to be in any order. If a key is missing, the get() will return null.

Comments

0

If you are only interested in the occupation value, you could do:

String s = "name:xxx occupation:yyy phone:zzz";
Pattern pattern = Pattern.compile(".*occupation:(\\S+).*");
Matcher matcher = pattern.matcher(s);
if (matcher.matches()){
    String occupation = matcher.group(1);
}

Comments

0
str = "name:xxx occupation:yyy phone:zzz
    name:xx1 occupation:yy3 phone:zz3
    name:xx2 occupation:yy1 phone:zz2"

name[0] = str.subtsring(str.indexAt("name:")+"name:".length,str.length-str.indexAt("occupation:"))
occupation[0] = str.subtsring(str.indexAt("occupation:"),str.length-str.indexAt("phone:"))
phone[0] = str.subtsring(str.indexAt("phone:"),str.length-str.indexAt("occupation:"))

Comments

0

I got the solution:

String[] temp= objValue.split("\n");
String[] temp1 = temp[1].split(":");
String Value = temp1[1].toString();
System.out.println(value);

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.