We can replace a sub string using replaceAll() of String class
To show this take one example:
Consider We want to change a string result count value given like 1,420,000 we would like to change it to long value we directly convert it to long it will throw java.lang.NumberFormatException.
To convert given string value to long we have to remove all , from above string then we will convert new string to long value
1 2 3 4 5 6 7 8 9 10 11 12 | public class Replace { public static void main(String s[]) { String count = "1,420,000"; System.out.println("Count formatted String value "+count); String tempCount = count.replaceAll(",", ""); System.out.println("Count String value "+tempCount); Long longCount = Long.parseLong(tempCount); System.out.println("Count long value "+longCount); } } |
Output:
1 2 3 4 5 | run: Count formatted String value 1,420,000 Count String value 1420000 Count long value 1420000 BUILD SUCCESSFUL (total time: 4 seconds) |