java - Extract 2 Integers from String


Keywords:java 


Question: 

String weapon = "pistol . <1/10>";
Result:
int clip = 1;
int ammo = 1;

format of the string = (WEAPON) . <(CLIP)/(AMMO)>
And I need the Clip and Ammo values.

So how I can extract those two values of the String. I can split it by "/" by it will still face: "gun <1" and "10>"
Thanks in advance


6 Answers: 

You can remove the non-digits characters like this:

String s = "pistol . <1/10>";
String[] numbers = s.replaceAll("^\\D+","").split("\\D+");

Now numbers[0] is 1 and numbers[1] is 10.

  • s.replaceAll("^\\D+","") removes the non digit characters at the beginning of the string, so the new string is now "1/10>"
  • .split("\\D+") splits on non digit characters (in this case / and >) and ignores the trailing empty strings if any

Alternatively, if the format is always exactly as you mentioned in your question, you could look for that specific pattern:

private final static Pattern CLIP_AMMO = Pattern.compile(".*<(\\d+)/(\\d+)>.*");

String s = "pistol . <1/10>";
Matcher m = CLIP_AMMO.matcher(s);
if (m.matches()) {
    String clip = m.group(1); //1
    String ammo = m.group(2); //10
}
 
String weapon = "pistol . <1/10>";

String str = weapon.substring(weapon.indexOf("<")+1,weapon.indexOf(">")); // str = "<1/10>"

int clip = Integer.parseInt(str.substring(0,str.indexOf("/"))); // clip = 1

int ammo = Integer.parseInt(str.substring(str.indexOf("/")+1)); // ammo = 10

clip = 1

ammo = 10

Done..

 
String weapon = "pistol . <1/10>";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(weapon); 
List<Integer> numbers = new ArrayList<Integer>();
while (m.find()) {
    numbers.add(Integer.parseInt(m.group()));
}
System.out.println("CLIP: " + numbers.get(0));
System.out.println("AMMO: " + numbers.get(1));
 

You can try this too...

Extracting the the values WEAPON, CLIP and AMMO from the String "(WEAPON) . <(CLIP)/(AMMO)>"

String str = "(WEAPON) . <(CLIP)/(AMMO)>";
Pattern pattern = Pattern.compile("\\((.*?)\\)");
Matcher matcher = pattern.matcher(str);
while(matcher.find()) {
   System.out.println(matcher.group(1));
}

Extracting the the values 1, 10 from the String "pistol . <1/10>":

List<String[]> numbers = new ArrayList<String[]>();
String str = "pistol . <1/10>";
Pattern pattern = Pattern.compile("\\<(.*?)\\>");
Matcher matcher = pattern.matcher(str);
while(matcher.find()) {
   numbers.add(matcher.group(1).split("/"));
}
 

And then u split the resultant substrings by "<" (gun <1) and ">" (10>)respectively

 

You can try something like this.

public static void main(String[] args) {
    String weapon = "pistol . <1/10>";
    String test = "";

    Pattern pattern = Pattern.compile("<.*?>");
    Matcher matcher = pattern.matcher(weapon);

    if (matcher.find())
        test = matcher.group(0);

    test = test.replace("<", "").replace(">", "");
    String[] result = test.split("/");

    int clip = Integer.parseInt(result[0]);
    int ammo = Integer.parseInt(result[1]);
    System.out.println("Clip value is -->" + clip + "\n"
            + "Ammo value is -->" + ammo);
}

Output:

Clip value is -->1
Ammo value is -->10