3

i have a string that has a int value in it. i just want to extract the int value from the string and print.

String str="No. of Days : 365";
String daysWithSplChar = str.replaceAll("[a-z][A-Z]","").trim();
char[] ch = daysWithSplChar.toCharArray();
StringBuffer stb = new StringBuffer();
for(char c : ch)
{
  if(c >= '0' && c <= '9')
   {
      stb.append(c);
   }
}

int days = Integer.ParseInt(stb.toString());

is there any better way than this. please let me know.

buttowski
  • 4,657
  • 8
  • 27
  • 33
  • possible duplicate of [How to convert string to int in Java?](http://stackoverflow.com/questions/5585779/how-to-convert-string-to-int-in-java) – Luiggi Mendoza May 21 '13 at 06:03
  • [This](http://stackoverflow.com/questions/13440506/java-find-all-numbers-in-the-string-need-check) may be of interest. – squiguy May 21 '13 at 06:04
  • The best method to convert String to Int in JAVA http://stackoverflow.com/a/5585800/1042240 – Ahmed Z. May 21 '13 at 06:05
  • @LuiggiMendoza Looks like he knows how to get the value already. – squiguy May 21 '13 at 06:05

4 Answers4

9

try String.replaceAll

    String str = "No. of Days : 365";
    str = str.replaceAll(".*?(\\d+).*", "$1");
    System.out.println(str);

you will get

365
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • @AndroidKiller, @psr `$1` = the first capturing group (~ pair of parentheses) – John Dvorak May 21 '13 at 06:09
  • it means replace all with what's in group 1 (expr in round brackets) – Evgeniy Dorofeev May 21 '13 at 06:11
  • The answer is having some problem. OP said there will be an integer in the string, without saying it will always be at the end of string. So this regex is not going to work if the string look like this: "There are 365 Days" – Adrian Shum May 21 '13 at 09:36
2

Another way of using regex (other than the way suggested by @EvgeniyDorofeev) which is closer to what you did:

str.replaceAll("[^0-9]","");   // give you "365"

which means, replace everything that is not 0-9 with empty string (or, in another word, remove all non-digit characters)

This is meaning the same, just a matter of taste which one is more comfortable to you:

str.replaceAll("\\D","");   // give you "365"
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
1
Scanner in = new Scanner("Hello123").useDelimiter("[^0-9]+");
int integer = in.nextInt();

This will get you the integer

Arun
  • 3,440
  • 11
  • 60
  • 108
1

following code gives you integer value

  String str = "No. of Days : 365";
        str = str.replaceAll(".*?(\\d+)", "$1");
        System.out.println(str);
        Integer x = Integer.valueOf(str);//365 in integer type
           System.out.println(x+1);//output 366
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
  • the key part of your answer is simply the same as @EvgeniyDorofeev's answer – Adrian Shum May 21 '13 at 09:14
  • yes . i have cover conversion part String to integer.. Integer x = Integer.valueOf(str); int y = Integer.parseInt(str); – KhAn SaAb May 21 '13 at 09:17
  • obviously OP has no problem in converting String to integer, as he have a mostly correct `int days = Integer.ParseInt(stb.toString());` in his question already – Adrian Shum May 21 '13 at 09:38
  • well, I don't see any difference on yours and his way, both are using Integer.parseInt(). The biggest difference is about extraction of the string which contains only digits, which you are simply copying from another answer.... – Adrian Shum May 21 '13 at 13:52
  • copying means what?.m not forcing anybody to use my code or accept my answer .the above answer is only extracting the integer value.i have put my conversion part which is not that answer. – KhAn SaAb May 21 '13 at 20:19