To convert a String
containing a number with commas as a thousands separator to a long
value in Java, you can use the replaceAll
method of the String
class to remove the commas, and then use the Long.parseLong
method to parse the String
as a long
value.
Here is an example of how you can convert a String
with commas to a long
value in Java:
String str = "1,000,000"; str = str.replaceAll(",", ""); long value = Long.parseLong(str);
In this example, we have a String
called str
that contains a number with commas as a thousands separator. We use the replaceAll
method to remove the commas from the String
, and assign the result back to the str
variable. We then use the Long.parseLong
method to parse the String
as a long
value, and assign it to a long
variable called value
.