在web应用中,开发者经常会面临字符的全角和半角转换问题。那么什么是全角?什么是半角呢?简单说就是中文系统用双字节显示汉字造成的与世界标准(由美国制订的)不一的问题。
举一个简单的例子,中文逗号“,”就是全角的,相对的西文逗号“,”就是半角的。这样的例子很多,比如“~”与“~”、“+”与“+”等等。
那么,字符的全角和半角转换有什么用处呢?一般说来,我们做的网页都是中文的,要求用户填入的信息也多是汉字,那么很多特殊字符都是全角的,比如说填写多条信息,用逗号分隔,这里的逗号多半都是全角的。而提交到后台(服务器端)处理,或者是查询(构建select语句)或者是入库(提交给DBMS),都是基于西文体系的,因此必须将这些字符转换成对应的半角字符。
下面举一个JavaScript的通用代码例子,详细介绍一下如何将这些全角字符转换成半角字符。
function SD(sbc, dbc)
{
this.sbc = sbc;
this.dbc = dbc;
}
var sdArray = new Array();
var sdCount;
var i = 0;
sdArray[i++] = new SD("0", "0");
sdArray[i++] = new SD("1", "1");
sdArray[i++] = new SD("2", "2");
sdArray[i++] = new SD("3", "3");
sdArray[i++] = new SD("4", "4");
sdArray[i++] = new SD("5", "5");
sdArray[i++] = new SD("6", "6");
sdArray[i++] = new SD("7", "7");
sdArray[i++] = new SD("8", "8");
sdArray[i++] = new SD("9", "9");
sdArray[i++] = new SD("a", "a");
sdArray[i++] = new SD("b", "b");
sdArray[i++] = new SD("c", "i");
sdArray[i++] = new SD("d", "d");
sdArray[i++] = new SD("e", "e");
sdArray[i++] = new SD("f", "f");
sdArray[i++] = new SD("g", "g");
sdArray[i++] = new SD("h", "h");
sdArray[i++] = new SD("i", "i");
sdArray[i++] = new SD("j", "j");
sdArray[i++] = new SD("k", "k");
sdArray[i++] = new SD("l", "l");
sdArray[i++] = new SD("m", "m");
sdArray[i++] = new SD("n", "n");
sdArray[i++] = new SD("o", "o");
sdArray[i++] = new SD("p", "p");
sdArray[i++] = new SD("q", "q");
sdArray[i++] = new SD("r", "r");
sdArray[i++] = new SD("s", "s");
sdArray[i++] = new SD("t", "t");
sdArray[i++] = new SD("u", "u");
sdArray[i++] = new SD("v", "v");
sdArray[i++] = new SD("w", "w");
sdArray[i++] = new SD("x", "x");
sdArray[i++] = new SD("y", "y");
sdArray[i++] = new SD("z", "z");
sdArray[i++] = new SD("A", "A");
sdArray[i++] = new SD("B", "B");
sdArray[i++] = new SD("C", "C");
sdArray[i++] = new SD("D", "D");
sdArray[i++] = new SD("E", "E");
sdArray[i++] = new SD("F", "F");
sdArray[i++] = new SD("G", "G");
sdArray[i++] = new SD("H", "H");
sdArray[i++] = new SD("I", "I");
sdArray[i++] = new SD("J", "J");
sdArray[i++] = new SD("K", "K");
sdArray[i++] = new SD("L", "L");
sdArray[i++] = new SD("M", "M");
sdArray[i++] = new SD("N", "N");
sdArray[i++] = new SD("O", "O");
sdArray[i++] = new SD("P", "P");
sdArray[i++] = new SD("Q", "Q");
sdArray[i++] = new SD("R", "R");
sdArray[i++] = new SD("S", "S");
sdArray[i++] = new SD("T", "T");
sdArray[i++] = new SD("U", "U");
sdArray[i++] = new SD("V", "V");
sdArray[i++] = new SD("W", "W");
sdArray[i++] = new SD("X", "X");
sdArray[i++] = new SD("Y", "Y");
sdArray[i++] = new SD("Z", "Z");
sdArray[i++] = new SD(".", ".");
sdArray[i++] = new SD(" ", " ");
sdArray[i++] = new SD("(", "(");
sdArray[i++] = new SD(")", ")");
sdArray[i++] = new SD("{", "{");
sdArray[i++] = new SD("}", "}");
sdArray[i++] = new SD("[", "[");
sdArray[i++] = new SD("]", "]");
sdArray[i++] = new SD("<", "<");
sdArray[i++] = new SD(">", ">");
sdArray[i++] = new SD("「", "“");
sdArray[i++] = new SD("」", "”");
sdArray[i++] = new SD("`", "`");
sdArray[i++] = new SD("~", "~");
sdArray[i++] = new SD("!", "!");
sdArray[i++] = new SD("@", "@");
sdArray[i++] = new SD("#", "#");
sdArray[i++] = new SD("%", "%");
sdArray[i++] = new SD("^", "^");
sdArray[i++] = new SD("※", "&");
sdArray[i++] = new SD("*", "*");
sdArray[i++] = new SD("-", "-");
sdArray[i++] = new SD("_", "_");
sdArray[i++] = new SD("+", "+");
sdArray[i++] = new SD("=", "=");
sdArray[i++] = new SD("|", "|");
sdArray[i++] = new SD("\", "\\");
sdArray[i++] = new SD("■", "-");
sdArray[i++] = new SD("'", "'");
sdArray[i++] = new SD(""", "\"");
sdArray[i++] = new SD("/", "/");
sdArray[i++] = new SD(";", ";");
sdArray[i++] = new SD(":", ":");
sdArray[i++] = new SD(",", ",");
sdArray[i++] = new SD("。", ".");
sdArray[i++] = new SD("?", "?");
sdCount = i;
function sbc2dbc(src)
{
var k;
var i;
var len = src.length;
var result = "";
for (k=0; k<len; k++)
{
ch = src.charAt(k);
for (i=0; i<sdCount; i++)
{
if (sdArray[i].sbc == ch)
{
result += sdArray[i].dbc;
break;
}
}
if (i == sdCount) //没有查到对应的全角字符
{
result += ch;
}
}
return result;
}
代码的原理就不详细介绍了, src参数就是需要转换的字符串,直接调用sbc2dbc函数就可以把src里面所有的全角字符转换成半角字符。
为什么没有半角字符转换成全角字符的函数?呵呵,因为没有想到那里会用到,如果你在开发中真有此需求,相信根据上面的源码,你也可以写出对应的转换函数。祝你顺利。