Results 1 to 13 of 13
- 10-26-2008, 01:14 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
-
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). Something like so:
Java Code:import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; public class Fubar001 { public static void main(String[] args) { String s1 = "100,000"; String s2 = ""; NumberFormat nf = NumberFormat.getInstance(Locale.getDefault()); // make locale-specific try { int i1 = nf.parse(s1).intValue(); s2 = String.valueOf(i1); System.out.println(s2); } catch (ParseException e) { e.printStackTrace(); } } }Last edited by Fubarable; 10-26-2008 at 02:28 PM.
- 10-26-2008, 02:46 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
These numbers with commas are embedded in existing strings that I didn't create and could be anywhere in the strings, so I need a general way to remove the commas in numbers.
thanks,
William
-
So let's first see your code that demonstrates your attempt to solve this, and then we'll then know where to go with this.so I need a general way to remove the commas in numbers.
- 10-26-2008, 04:19 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
I'd like to simply use a regular expression in String.replaceAll.
String str = "there are 123,000 blue items and 117,287 red items";
String regex = "???????";
str = str.replaceAll(regex, str);
After this, str would be "there are 123000 blue items and 117287 red items"
I'm just learning regular expressions and don't know how to create such a regular expression. That's my question.
William
-
I'm just learning regex myself, but I think that to solve this you'll need both look aheads and look behinds. You may wish to try this regex String:
something like:Java Code:String regex = "(?<=[\\d])(,)(?=[\\d])";
To Darryl.Burke: Hey, I may be able to learn some of this regex stuff after all!Java Code:import java.util.regex.Matcher; import java.util.regex.Pattern; public class Fubar002 { public static void main(String[] args) { String regex = "(?<=[\\d])(,)(?=[\\d])"; Pattern p = Pattern.compile(regex); String str = "there are 123,000 blue items and 117,287 red items"; Matcher m = p.matcher(str); str = m.replaceAll(""); System.out.println(str); } }
- 10-27-2008, 10:33 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
Thanks, that works. I need to learn about look aheads now that I know what's involved.
William
- 10-27-2008, 01:48 PM #8
I took a slightly different approach. I used capture groups to repeatedly scan the String for text before the , the , and text after the ,. The before text must end with digit and the after text must start with 3 digits.
Java Code:public static void main(String[] args) { final String data = "This short, quick test for removing , from 1,235,789 and more."; Pattern pattern = Pattern.compile("(.*?\\d+)(,)(\\d{3}.*?)"); // Defines 3 groups (in parans): // 1 - before the , // 2 - the , // 3 - after the comma String newData = data; // Prime the work variable while(true) { Matcher matcher = pattern.matcher(newData); if(matcher.find()) { int gc = matcher.groupCount(); for(int i=0; i <= gc; i++) { System.out.println("group" + i + "=" + matcher.group(i)); //group0=This short, quick test for removing , from 1,235 //group1=This short, quick test for removing , from 1 //group2=, //group3=235 } newData = matcher.replaceFirst("$1$3"); System.out.println(" new=" + newData + "\ndata=" + data); // new=This short, quick test for removing , from 1235 and more. //data=This short, quick test for removing , from 1,235 and more. // new=This short, quick test for removing , from 1235789 and more. <<< Final result //data=This short, quick test for removing , from 1,235,789 and more. }else{ break; // Exit loop when no match found } } // end while() } // end mainLast edited by Norm; 10-27-2008 at 03:25 PM. Reason: Correct error
- 10-28-2008, 08:39 PM #9
I like keeping it as simple as possible. The only condition to match here is a comma preceded and follwed by a digit character.
This is alomost the same as the solution already posted by Fubarable, but uses String#replaceAll which calls the same Pattern and Matcher methods under the hood.dbJava Code:public class RemoveComma { public static void main(String[] args) { String regex = "(?<=\\d),(?=\\d)"; String input = "there are 123,000 blue items and 117,287 red items"; String output = input.replaceAll(regex, ""); System.out.println(output); } }
-
Thanks Darryl, you're the man!
- 03-05-2012, 09:07 AM #11
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: Getting rid of commas in large numbers?
Hi all. I'm a new member. I just wanted to thank you all for this solution! I was beating my head on this problem all day lol. I went from string handling library and methods then it let me to RegEx and then i gave up and typed in "how to get rid of commas in large numbers" and it brought me here.
I am a member and a fan now and just want to thank you guys for this solution that apparently works for me:
- 03-05-2012, 09:09 AM #12
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: Getting rid of commas in large numbers?
...by the way, i have found a lot of java reg expression tutorials on line - a lot of them were not so good.
Can anyone recommend a good resource?
- 03-05-2012, 10:35 AM #13
Re: Getting rid of commas in large numbers?
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
In future, when you have a question, ask it in a new thread -- they're free. Don't resurrect old dead threads, and especially not with a new, different question.
The question in this thread was about removing commas from a String -- not about where to find a good regex tutorial.
Closing the thread.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
parsing/storing large text data
By hkansal in forum New To JavaReplies: 4Last Post: 10-19-2008, 06:34 PM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Need to find large files and folder on the PC. What app needed?
By Cleaner007 in forum Reviews / AdvertisingReplies: 1Last Post: 09-30-2008, 07:06 PM -
Eclipse with VERY LARGE source trees
By wyrickre in forum EclipseReplies: 0Last Post: 02-01-2008, 02:23 AM -
How to extract each record from a large XML doc. which contain multiple nodes
By renjan in forum XMLReplies: 1Last Post: 07-26-2007, 07:28 PM


LinkBack URL
About LinkBacks


Bookmarks