Several efficient and concise character processing functions

Source: Internet
Author: User

All extensions Based on String. prototype:
The reason is that a netizen discussed two functions with me,
One is isDateTime (judge whether the character Meets the yyyy-mm-dd hh: mm: ss Date Format)
The other is the left function, which is similar to the left function of vbscript to achieve mixed interception of Chinese and English characters.
Both of these functions use loops and N-plus if statements. Each function has more than 40 lines of code and can be simplified.
As a result, I wrote the following code. I dare not say it is the most efficient, but it is enough to simplify it. The left function is only one line.
 Copy codeThe Code is as follows:
1 <script type = "text/javascript">
2
3 // by Go_Rush (A Shun) from http://ashun.cnblogs.com/
4
5 function $ A (arrayLike ){
6 for (var I = 0, ret = []; I <arrayLike. length; I ++) ret. push (arrayLike [I])
7 return ret
8 };
9 Array. prototype. any = function (f ){
10 for (var I = 0; I <this. length; I ++) if (f (this [I], I, this) return true;
11 return false
12 };
13
14
15
16 // determine whether the string conforms to the date format of yyyy-mm-dd hh: mm: ss. The format is correct and the leap year and month must also be correct.
17
18 String. prototype. isDateTime = function (){
19 try {
20 var arr = (this. length = 19 )? This. split (/\ D/): []
21 -- arr [1]
22 eval ("var d = new Date (" + arr. join (",") + ")")
23 return Number (arr [0]) = d. getFullYear () & Number (arr [1]) = d. getMonth ()
24 & Number (arr [2]) = d. getDate () & Number (arr [3]) = d. getHours ()
25 & Number (arr [4]) = d. getMinutes () & Number (arr [5]) = d. getSeconds ()
26} catch (x) {return false}
27}
28
29 /*
30 alert ("10:10:40". isDateTime () // true
31 alert ("10:10:40". isDateTime () // false
32 alert ("2002-22-31 10:10:40". isDateTime () // false
33 alert ("2002-22-31 30: 10: 40". isDateTime () // false
34 */
35
36
37 // check whether it ends with a specific string
38 String. prototype. startsWith = function (){
39 var _ string = this
40 return $ A (arguments). any (function (value) {return _ string. slice (0, value. length) = value })
41 };
42 /*
43 alert ("http://www.google.com /". startsWith ("http: //", "ftp: //", "telnet: //") // if either of the values is true, true is returned.
44 alert ("http://www.google.com/". startsWith ("https: //", "file: //") // false
45 alert ("abc". startsWith ("a") // true
46 */
47
48
49 // check whether it ends with a specific string
50 String. prototype. endsWith = function (){
51 var _ string = this
52 return $ A (arguments). any (function (value) {return _ string. slice (value. length * (-1) = value })
53 };
54
55
56
57 // extract n characters from the left. If the Chinese character is contained, the Chinese character is calculated as two characters.
58 String. prototype. left = function (n ){
59 return this. slice (0, n-this.slice (0, n). replace (/[\ x00-\ xff]/g, ""). length)
60 };
61 /*
62 alert ("abcdefg". left (3) = "abc ")
63 alert ("Chinese cdefg". left (5) = "China ")
64 alert ("China abcdefg". left (5) = "China ")
65 */
66
67
68
69
70 // extract n characters from the right side. If the Chinese character is contained, the Chinese character is calculated as two characters.
71 String. prototype. right = function (n ){
72 return this. slice (this. slice (-n). replace (/[\ x00-\ xff]/g, ""). length-n)
73 };
74
75 /*
76 alert ("abcdefg". right (3) = "efg ")
77 alert ("cdefg Chinese". right (5) = "Chinese ")
78 alert ("abcdefg China". right (5) = "g China ")
79 */
80
81 </script>

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.