1

I have the following string in Java. For Example:

String abc = "nama=john; class=6; height=170; weight=70";

How can I extract the value of height from the String?

Outputs: height=170

This is the code I have written so far:

String abc = "nama=john; class=6; height=170; weight=70";
String[] tokens = abc.split("; ");
List<String> listString = new ArrayList<String>();
String mod = new String();

for (String s : tokens) {
    mod = s;
    System.out.println(s);
    listString.add(mod);
}

System.out.println(listString.size());

But I do not get the value height. Instead, I get value of height as a String.

Thanks.

2
  • 6
    is there any approach that you tried? do you have this structure of string certain? take a look at regular expressions Commented Aug 19, 2015 at 8:06
  • 1
    or you can use substring and indexOf. Commented Aug 19, 2015 at 8:08

6 Answers 6

3

With this Code-Snippet:

String abc = "nama=john; class=6; height=170; weight=70";
for(String sa : abc.split(";")){
    System.out.println(sa.trim());
}

you generate this output:

nama=john
class=6
height=170
weight=70

if you want to add a specific String into a list you put the sa.trim() at the List.add parameter. To find the height-String you can use: if(sa.trim().startsWith("height")) and you have the needed String.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @mrt i get value height. i add bellow code. for(String abc :listString){ if(abc.trim().startsWith("height")){ System.out.println(abc); } } And i get value. Thanks a lot.
+1... He answered it itself afterwards (stackoverflow.com/a/32090286/1466583). I placed already a comment there. Hope he'll see it
2

you can use this regex:

(?<=height=)(\d+)(?=;|\Z)

if you want to implement this, you can do it like this:

Pattern pattern = Pattern.compile("(?<=height=)(\\d+)(?=;|\\Z)");

// create matcher object.
Matcher m = pattern.matcher(abc);

if (m.find()) 
{
    String height =  m.group(0);
} 
else 
{
    System.out.println("not found");
}

here, you have an example: https://regex101.com/r/iM3gY0/2
and here you have an executable snipped: https://ideone.com/azngNt


If you want all parameter, you can use this regex:

(\w+)=([\d|\w]+)(?=;|\"|\Z)

so you get as Pattern:

Pattern pattern = Pattern.compile("(\\w+)=([\d|\\w]+)(?=;|\\"|\\Z)");

and the Regex101 again: https://regex101.com/r/uT6uK1/3

Comments

0

@Fast Snail you are correct, but I think they wanted an integer value of it:

final String string = "nama=john; class=6; height=170; weight=70";
final String[] tokens = string.split("; ");

for (String token : tokens) {
    if (token.contains("height")) {
        System.out.println(token);
        final String[] heightSplit = token.split("=");
        Integer heightValue = new Integer(heightSplit[1]);
        System.out.println("height=" + heightValue);
    }
}

System.out.println(tokens.length);

This should do what you need.

Comments

0

Use String tokenizer

import java.util.StringTokenizer;


public class stringval {



    StringTokenizer st = new StringTokenizer(" nama=john, class=6, height=170, weight=70;",",");{

while(st.hasMoreTokens()){

        String abc=st.nextToken();
        if(abc.equals("height=170")){
            StringTokenizer s=new StringTokenizer(abc,"=");
            while(s.hasMoreTokens()){
            String str=s.nextToken();
            if (s.equals("170")){
                System.out.print(s);
                break;
            }


            }
     }
}

    }
}

Comments

0

With Java 8 you can do the following:

Map<String, String> map = Arrays.stream("nama=john; class=6; height=170; weight=70".split(";"))
            .map(s -> s.trim().split("="))
            .collect(Collectors.toMap(s -> s[0], s -> s[1]));

System.out.println(map.get("height")); //170

Comments

-1

Thanks Mr. MrT.

Code answer: Resolved.

String abc = "nama=john; class=6; height=170; weight=70";
String[] tokens = abc.split("; ");
        List<String> listString = new ArrayList<String>();
        String mod = new String();

        for(String s:tokens){
            mod =s;
            System.out.println(s);
            listString.add(mod);
        }

        System.out.println(listString.size());

for(String abcd :listString){
            if(abcd.trim().startsWith("height")){
                System.out.println(abcd);
            }
        }

1 Comment

remember marking @mrt 's answer as accepted answer, to give him the credits. How you finally implemented it, hasn't be posted here

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.