-1

I have a String/Value "2.450,00. I want to get the Integer value from it. For that, I need to remove the " in front of the 2.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
id6070
  • 1
  • 1
  • 2
  • https://stackoverflow.com/questions/13386107/how-to-remove-single-character-from-a-string – MMG May 09 '20 at 09:19
  • https://stackoverflow.com/questions/19299788/how-to-replace-double-quotes-in-a-string-with-in-java – Rakesh Soni May 09 '20 at 09:20
  • 1
    This seems to be a duplicate of [Java - removing first character of a string](https://stackoverflow.com/questions/4503656/java-removing-first-character-of-a-string). – Salem May 09 '20 at 09:20
  • str.replace("\"",""); will not work for you? – aatif May 09 '20 at 09:21

4 Answers4

1

First you want to remove the comma sign and the decimals since an int (Integer) is a whole number.

str = str.split(",")[0];

This will split the string on the "," and take the first index which contains the string "2.450". Then you want to remove the punctuation. This can be done by replacing everything which is not a number with an empty space "".

str = str.replaceAll("[^0-9]", "");

Lastly, you want to convert the string to an integer.

int strAsInt = Integer.parseInt(str);

Full code:

public static void main(String[] args) {
    // The initial value
    String str = "2.450,00";

    str = str.split(",")[0];

    str = str.replaceAll("[^0-9]", "");

    int strAsInt = Integer.parseInt(str);

    // This will print the integer value 2450
    System.out.println(strAsInt);
}

One liner:

int stringAsInt = Integer.parseInt(str.split(",")[0].replaceAll("[^0-9]", ""));
ANicholasson
  • 477
  • 5
  • 19
0

Try the below snippet. Use substring method to remove "

public static void main(String[] args) {
  String str = "\"2.450,00";

  // prints the substring after index 1 till str length
  String substr = str.substring(1);
  System.out.println("substring = " + substr);

}
Vinetos
  • 150
  • 1
  • 7
0

At first, you need to remove any non-digit character (excluding the '.' and ',' characters) from the input string, the regex "[^\\d\\,\\.]" may help you with this:

final String inputString = "Total: 100.000,05$";
final String numberString = inputString.replaceAll("[^\\d\\,\\.]", "");

The next step is a formatting digit string into a valid double form. At this step, you'll need to replace '.' with an empty string and the ',' with a dot '.'.

final String formattedNumberString = numberString
                                      .replace(".","")
                                      .replace(',', '.');

And, in the end, you are finally able to parse a valid number:

double inputNumber = Double.parseDouble(formattedNumberString);
-1

(1) Remove double quote from string

String str = "\"2.450,00";

str = str.replace("\"", "")

(2) then you should replace the comma as well

str = str.replace(",", "")

(3) Finally you should use the Integer.parseInt() method to convert String to Integer

int i = Integer.parseInt(str);
Rakesh Soni
  • 10,135
  • 5
  • 44
  • 51