Daniel Holler Daniel Holler - 7 months ago 21
Java Question

Find first sequence of numbers in a string?

So first of all i would like to clear things off by saying that this is for a Minecraft plugin. So I have a method to spawn some mobs (You dont know what mobs it is), and give them custom names. The names are based on numbers. But I also have a sequence of characters in its name. So for instance if the name for the mob was "355 Blaze" it would return an int of 355, and cut the rest out. How should I do this? Currently I use substring but it doesnt work as if the number goes above 9 it will return the first number only.

Answer

If its separated by space, use substring based on the location of first space:

Integer mobId = new Integer(fullMobName.substring(0, fullMobName.indexOf(" "))); 
Comments