Search code examples
javastring-parsing

Get an Integer that can be modified from a String?


So, the full String that I'm trying to read is:

Members online: (0/0):
Members offline: (0/0):

While I think I could substring it to get the 0, it can change it's size, because it's an int... Is there any way I could get the integers even if they change? Thanks.

EDIT: I also want to only get the first integer of "0/0"... Any ways I could do this? I know I could use regex but I don't know if it's the best way to do it...


Solution

  • You should use indexOf and lastIndexOf for that purpose.

    int n1 = Integer.parseInt(t.substring(t.indexOf('(') + 1, t.indexOf('/')))
    

    That variable (n1) will save the number itself, if you want just the position you should use next one.

    int pos=t.indexOf('(') + 1