首先看看判断字符串长度的几种方法(英文占1个字符,中文汉字占2个字符) 方法一: function strlen(str) { var len = 0; for (var i = 0; i < str.length; i++) { var c = str.charCodeAt(i); //单字节加1 if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) { len++
目的:计算字符串长度(英文占1个字符,中文汉字占2个字符) 方法一: 代码如下: String.prototype.gblen = function() { var len = 0; for (var i=0; i<this.length; i++) { if (this.charCodeAt(i)>127 || this.charCodeAt(i)==94) { len += 2; } else { len ++;
方法1: String.prototype.gblen = function() { var len = 0; for (var i=0; i<this.length; i++) { if (this.charCodeAt(i)>127 || this.charCodeAt(i)==94) { len += 2; } else { len ++; } } return len; } 方法2: function strlen(str){ var len = 0; for (var i=0; i&
test.sh #!/bin/bash s1="" if test $s1 ;then echo "length is not zero" else echo "the length is 0" fi s2="shell" if test $s2 ;then echo "length is not 0" else echo "the length is 0" fi 执行 sudo chm
test.sh #!/bin/bash echo "enter the string:" read filename if test -z $filename ; then echo "the length is 0" else echo "the length is not 0" fi 执行 sudo chmod +x test.sh./test.sh 输出 enter the string: the length 执行 ./test.sh 输