[Summary] efficient string extraction numeric methods produced by Spads-the last day of Christmas!

Source: Internet
Author: User

Produced by Spads by WangXP and Shane


Directory
----------------------------------------
1 Summary: http://shanelooli.blog.51cto.com/5523233/1100063
2 Program http://shanelooli.blog.51cto.com/5523233/1100074
3 function http://shanelooli.blog.51cto.com/5523233/1100356
4 Performance Test Program http://shanelooli.blog.51cto.com/5523233/1100417
_ Test Analysis http://shanelooli.blog.51cto.com/5523233/1100418
5 Appendix]

Due to the length of the article, BKJIA can only contain 80000 characters, so this article will be split into several parts for release.
Among them, the appendix 5 is for 1 Summary, so it is put together.


1 Summary]
----------------------------------------
Recently, as we continue to write formula processing and Json-Java interchange components, we have made and optimized the tool to convert strings into numeric values, in terms of performance, Apache has made achievements in the way of Apache tools and Java class libraries. The basic idea of this method is to scan by character. At present, I have summarized more than a dozen articles about converting strings into numeric values, which are generally repeated, including these methods. The website I have read is collected in the appendix.

1. Use Character # isDigit (char) provided by the Java system to determine whether it is a numerical value and then convert it.
2. Use a regular expression to determine whether it is a numerical value and then convert it.
3. Direct conversion to capture exceptions.
4. Apache provides isNumeric (String) and isNumericSpace (String) methods for determination. If the conversion is a numeric value.

Currently, we need to support integers, spaces between numbers, blank headers and tails, decimal places, and decimal places starting with a decimal point.

The isDigit (char) of JavaLib can only judge numbers, so spaces, first negative signs, and first decimal points must be removed in advance.
Regular Expressions need to remove spaces before conversion
Remove spaces before direct conversion.
The Apache tool can determine numbers and spaces. Therefore, the first negative number and the first decimal point must be removed in advance.

The Spads program directly uses character characteristics to scan strings. It can also be considered as a method that combines preprocessing and character ASCII judgment. This method parses a string that can be parsed into a value. Otherwise, null is returned.

┌ ── ─
│ The following are 51CTO exclusive │
└ ── ─
Why do we need to support this? Because in actual situations, we needRespect users' habits.

1. The number is very long.
When I write the price, will someone frequently write 28,350,000? This method is often used in practice. Generally, there are two types: comma-separated, and four-separated spaces. Aren't we four Chinese people coming in. In order to take the opportunity to promote the Chinese digital thinking, I only support four spaces, but not three commas. This requires that "spaces between numbers" be supported ". For example, 380 billion, 0.5 million, 3800, and 0050 respectively.

2. Decimal abbreviation
In many cases, decimal places are directly used as the starting character. Even the Double. parseDouble (String) of Java) Class Library supports this method .. 38 is it easier to write than 0.38?

3. empty head and tail caused by reasons
If a string is not generated by a program but is read by a certain input device, leading to leading spaces or '\ t' or even carriage return characters. This situation must be handled to prevent the conversion of numeric values from being affected.

 

5 Appendix]
----------------------------------------
Google searches for "Java to determine whether a string is a numerical value"
Toutiao http://www.blogjava.net/Javaphua/archive/2012/05/18/122131.html
Display method most http://javapub.iteye.com/blog/666544
Direct transfer http://zhidao.baidu.com/question/38323159
Non-innovative http://www.ideagrace.com/html/doc/2006/04/28/00784.html
Apache tools http://blog.csdn.net/jojoy_828/article/details/2837784
Comparison full http://blog.csdn.net/bluesuperman/article/details/1840952
Non-innovative http://www.cnblogs.com/ztf2008/archive/2009/02/19/1393744.html
Non-innovative http://wenwen.soso.com/z/q198258851.htm

Google searches for "Java to extract values from strings"
Toutiao http://blog.csdn.net/wangjinyu501/article/details/7625636
Non-innovative http://bbs.csdn.net/topics/340240275
Plural, but my demand does not involve http://www.bcw52.com/JAVA/2471.html

Google searches for "converting a Java string to a value"
Http://dongdong1314.blog.51cto.com/389953/79385

CSDN searches for "Java to extract values from strings"
There is a detailed description of the http://blog.csdn.net/wowwow_cai/article/details/4590553 for the conversion after the value is determined
Same as Google http://blog.csdn.net/wangjinyu501/article/details/7625636


==========================================================
This article is also published in my other technical logs
Http://blog.csdn.net/shanelooli/article/details/8432250 (CSDN)
ITeye: http://surmounting.iteye.com/blog/1754323
China open source community: http://my.oschina.net/shane1984/blog/98088

This article is from the "Shane from Spads" blog, please be sure to keep this source http://shanelooli.blog.51cto.com/5523233/1100063

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.