1

Is there a way to extract values from String.format() string. for example i created a string like below.

String str = String.format("a number %d, a name %s", 3, "sam");
System.out.println(str);  // output : a number 3, a name sam

Now given a similar string.

String str1 = "a number 45, a name joseph"
Object[] objects = // String.extract("a number %d, a name %s", str1); //something like this
expected output : [45, "joseph"]
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Farhan C K
  • 1,159
  • 18
  • 35
  • 1
    Does this answer your question? [Parsing formatted string](https://stackoverflow.com/questions/1410012/parsing-formatted-string) – amer Jan 07 '20 at 15:56
  • Yep, what if the pattern is "a name %s %s" ... how would you know whether "John William Doe" should turn into "John William" and "Doe", or "John" "William Doe"? So: simple patterns, you could turn into regexes, and then use that for matching, as outlined in that duplicate. – GhostCat Jan 07 '20 at 15:59
  • There is a trick you can use is juste spliting string with regex "a number|, a name" which will give you "45" and "Joseph" and then convert 45 from Integer.parseInt("45") – midugh Jan 07 '20 at 16:01
  • @R.LM But that only works for such simple examples ... – GhostCat Jan 07 '20 at 16:10

2 Answers2

0

You can use a regex to get what you want. For this example the regex: a number ([0-9]*), a name ([A-Za-z]*) would return the number and the name for the string "a number 45, a name joseph".

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • But that means manual inspection of the format pattern, to create a regex from that. – GhostCat Jan 07 '20 at 15:56
  • Well, unless you know the format (and then you can create a regex or split it to extract the info you want) I don't think there is another way. That information forms part of the string from that moment on. – jeprubio Jan 07 '20 at 16:05
  • 1
    Well, he suggests that he knows the format pattern. That is the whole point of the question. But as written up above, turning the pattern into a regex isnt a perfect solution, but something to start with (given that there is no perfect solution that works for the generic case). – GhostCat Jan 07 '20 at 16:10
0

The values that you added to the String are now part of the String, so there is no way to "reverse" it.

Otherwise, if you know that the given string is going to follow a certain pattern such like

"a number " + digits here + ", a name " + a word here

I would suggest you to use regex:

This expression:

 /number ([0-9]*)/

will return the number as group 1.

And this one:

/, a name (\w*)/

will return the name again as group 1.

You can merge both regex in one expression such like:

/a number ([0-9]*), a name (\w*)/

Which will return the number as group 1 and the name as group 2.

So this code should do the trick:

 Matcher m = p.matcher("a number ([0-9]*), a name (\w*)");

 if (m.find()) {
    System.out.println(m.group(0)); // number
    System.out.println(m.group(1)); // name
 }

Also, if you want to approach the posibility of having surnames, you should change \w properly.

If you need them on an array refer to this post.

Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45