0

My input String is as below,

{"string name"=hi;"id"=44401234;"string name"=hey;"id"=87695432.....}

I want only the ids as output like, {44401234 87695432}

        for(int i=0;i<input.length();i++)
          {
               int index=input.indexOf("id");
               hh=input.substring(index+4,index+12);
               System.out.println(hh);
          }
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
happs
  • 417
  • 6
  • 15

1 Answers1

0

The below code uses Regex to get the value associated with id

try

import java.util.regex.Matcher;
import java.util.regex.Pattern;

      public class Test {
            public static void main(String[] args) {
                String input = "{\"string name\"=hi;\"id\"=44401234;\"string name\"=hey;\"id\"=87695432.....}";
                Pattern pattern = Pattern.compile("\"id\"=[0-9]{8}");
                Matcher m = pattern.matcher(input);
                while (m.find()) {
                    String str = m.group();
                    str = str.replace("\"id\"=", "");
                    System.out.println(str);
                }
            }

        }
upog
  • 4,965
  • 8
  • 42
  • 81