Here is your answer:

    String regex = "(?<=[\\d])(,)(?=[\\d])";
    Pattern p = Pattern.compile(regex);
    String str = "Your input";
    Matcher m = p.matcher(str);
    str = m.replaceAll("");
    System.out.println(str);

This only affects NUMBERS, not strings, as you asked.

Try adding that in your main method. Or try this one, it receives input:

       String regex = "(?<=[\\d])(,)(?=[\\d])";
        Pattern p = Pattern.compile(regex);
        System.out.println("Value?: ");
            Scanner scanIn = new Scanner(System.in);
            String str = scanIn.next();
        Matcher m = p.matcher(str);
        str = m.replaceAll("");
        System.out.println(str);
Answer from Whippet on Stack Overflow
๐ŸŒ
Blogger
javahungry.blogspot.com โ€บ 2023 โ€บ 07 โ€บ remove-comma-from-number.html
Remove Comma from Number in Java [2 ways] | Java Hungry
We can easily remove the comma from a number using replaceAll() method in Java as shown below in the example: public class RemoveCommaFromNumber2 { public static void main(String args[]) { String str2 = "123,456,789,0987,65"; str2 = str2.replaceAll(",",""); System.out.println("Remove comma ...
Discussions

exception - How to parse number string containing commas into an integer in java? - Stack Overflow
0 How to convert string to integer when we have comma (,) in between word when used in java code ... 0 How to properly import a float/double number being a String in a CSV file to a program as float/double? 0 Selenium WD | Exception error appears after using parsing of String into double ... Why did God establish the New Covenant in a way that did not remove the Jewish objection from ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Removing Dollar and comma from string - Stack Overflow
How can we remove dollar sign ($) and all comma(,) from same string? Would it be better to avoid regex? String liveprice = "$123,456.78"; More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - To remove commas the end of list of numbers - Stack Overflow
The question is to print out the elements having even actual position of even index starting from 1 and their sum in the following format.For example, if number of elements is 6 and the elements ar... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - how to remove a comma in a string - Stack Overflow
In your last question you wanted to add commas. Now you want to delete them. Maybe you should describe what you are actually trying to achieve... ... yes my customer wants to see number in comma separation but I have to do arithmetic operations on it, so I have to drop commas More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Quora
quora.com โ€บ How-can-commas-be-removed-from-an-integer-value-in-Java
How can commas be removed from an integer value in Java? - Quora
Answer: An int value contains a given number of bits - it does not have array of characters for a comma to be represented in, and so commas canโ€™t exist to be removed in an int or Integer in java. If you want to parse a String to remove commas before trying to turn it into an int value, try [cod...
๐ŸŒ
Brainmass
brainmass.com โ€บ computer-science โ€บ java โ€บ java-program-remove-comma-number-532817
Java: Program to remove comma from the number
Java: Program to remove comma from the number
The solution follows the hint provided in the posting. It accepts one number from the user and prints it after removing comma, if any, from it, and then exits.
Price ย  $2.49
๐ŸŒ
Sourcecodeera
sourcecodeera.com โ€บ blogs โ€บ Samath โ€บ Java-program-that-remove-comma-from-a-number.aspx
Java program that remove comma from a number
February 10, 2021 - import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String number; String prefix; String postfix; System.out.print("Number (between 1,000 - 999,999, including comma): "); number = input.next(); input.close(); prefix = number.substring(0, number.length() - 4); postfix = number.substring(number.length() - 3); System.out.println(prefix + postfix); } }
๐ŸŒ
Java-forums
java-forums.org โ€บ advanced-java โ€บ 12784-getting-rid-commas-large-numbers.html
Getting rid of commas in large numbers?
October 26, 2008 - I would try to make this Locale-specific by using a NumberFormat object. This way you could easily change this to be able to accommodate other ways of writing numbers (for instance Spain where they use a decimal point where you have a comma here).
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ core java โ€บ remove comma from string in java
Remove Comma from String in Java - Java2Blog
February 2, 2022 - You can use Stringโ€™s replace() method to remove commas from String in java.
Find elsewhere
๐ŸŒ
Programming.Guide
programming.guide โ€บ java โ€บ remove-trailing-comma-from-comma-separated-string.html
Java: Removing trailing comma from comma separated string | Programming.Guide
In Java, difference between default, public, protected, and private ... Executing code in comments?! ... This handles the empty list (empty string) gracefully, as opposed to lastIndexOf / substring solutions which requires special treatment of such case. Note that this assumes that the string ends with , (comma followed by space).
Top answer
1 of 5
2

There is a caveat that you need to be aware about since you're trying to use an intermediate int variable:

  • The range of double values is far broader than the range of long (and int obviously). So by converting a double into a long and then again to double you might lose the data.

Here are some ways how it can be done without losing the data:

1. Modular division:

double num = 59.012;

double wholeNum2 = num - num % 1;

2. Static method Math.floor():

double num = 59.012;

double wholeNum = Math.floor(num);

3. DecimalFormat class, that allow to specify a string pattern and format a number accordingly:

double num = 59.012;

NumberFormat format = new DecimalFormat("0");
double wholeNum = Double.parseDouble(format.format(num)); // parsing the formatted string

All examples above will give you the output 59.0 is you print the variable wholeNum.

When you need to obtain a double value as a result with the fractional part dropped, options 1 and 2 are preferable. But a string representing this number will still contain a dot and one zero .0 at the end.

But if you need to get the result as a String containing only the integer part of a double number, then DecimalFormat (as well String.format() that was mentioned in the comments) will help you to get rid of the fractional part completely.

NumberFormat format = new DecimalFormat("0");

System.out.println(format.format(59.012));

Output:

59
2 of 5
1

This is what i would do.

double num1 = 2.3;
double num2 = 4.5;

Later...

    int num11 = (int) num1;
    int num22 = (int) num2;
    System.out.println(num11 + ", " + num22 + ".");

The result would be:

2, 4.

That's all.