Chinaunix首页 | 论坛 | 博客
  • 博客访问: 93532
  • 博文数量: 14
  • 博客积分: 2070
  • 博客等级: 大尉
  • 技术积分: 185
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-31 11:07
文章分类
文章存档

2011年(1)

2009年(4)

2008年(9)

分类: Java

2008-07-31 15:57:11

public static class SD
    {
        private char sbc;
        private char dbc;

        public SD(char sbc, char dbc)
        {
            this.sbc = sbc;
            this.dbc = dbc;
        }

        public char getSBC()
        {
            return this.sbc;
        }

        public char getDBC()
        {
            return this.dbc;
        }
    }

    public static final SD[] sdArray = {
        new SD('0', '0'),
        new SD('1', '1'),
        new SD('2', '2'),
        new SD('3', '3'),
        new SD('4', '4'),
        new SD('5', '5'),
        new SD('6', '6'),
        new SD('7', '7'),
        new SD('8', '8'),
        new SD('9', '9'),
        new SD('a', 'a'),
        new SD('b', 'b'),
        new SD('c', 'c'),
        new SD('d', 'd'),
        new SD('e', 'e'),
        new SD('f', 'f'),
        new SD('g', 'g'),
        new SD('h', 'h'),
        new SD('i', 'i'),
        new SD('j', 'j'),
        new SD('k', 'k'),
        new SD('l', 'l'),
        new SD('m', 'm'),
        new SD('n', 'n'),
        new SD('o', 'o'),
        new SD('p', 'p'),
        new SD('q', 'q'),
        new SD('r', 'r'),
        new SD('s', 's'),
        new SD('t', 't'),
        new SD('u', 'u'),
        new SD('v', 'v'),
        new SD('w', 'w'),
        new SD('x', 'x'),
        new SD('y', 'y'),
        new SD('z', 'z'),
        new SD('A', 'A'),
        new SD('B', 'B'),
        new SD('C', 'C'),
        new SD('D', 'D'),
        new SD('E', 'E'),
        new SD('F', 'F'),
        new SD('G', 'G'),
        new SD('H', 'H'),
        new SD('I', 'I'),
        new SD('J', 'J'),
        new SD('K', 'K'),
        new SD('L', 'L'),
        new SD('M', 'M'),
        new SD('N', 'N'),
        new SD('O', 'O'),
        new SD('P', 'P'),
        new SD('Q', 'Q'),
        new SD('R', 'R'),
        new SD('S', 'S'),
        new SD('T', 'T'),
        new SD('U', 'U'),
        new SD('V', 'V'),
        new SD('W', 'W'),
        new SD('X', 'X'),
        new SD('Y', 'Y'),
        new SD('Z', 'Z'),
        new SD('.', '.'),
        new SD(' ', ' '),
        new SD('(', '('),
        new SD(')', ')'),
        new SD('{', '{'),
        new SD('}', '}'),
        new SD('[', '['),
        new SD(']', ']'),
        new SD('<', '<'),
        new SD('>', '>'),
        new SD('「', '“'),
        new SD('」', '”'),
        new SD('`', '`'),
        new SD('~', '~'),
        new SD('!', '!'),
        new SD('@', '@'),
        new SD('#', '#'),
        new SD('%', '%'),
        new SD('^', '^'),
        new SD('※', '&'),
        new SD('*', '*'),
        new SD('-', '-'),
        new SD('_', '_'),
        new SD('+', '+'),
        new SD('=', '='),
        new SD('|', '|'),
        new SD('\', '\\'),
        new SD('■', '-'),
        new SD('’', '\''),
        new SD('‘', '\''),
        new SD('“', '\"'),
        new SD('”', '\"'),
        new SD('/', '/'),
        new SD(';', ';'),
        new SD(':', ':'),
        new SD(',', ','),
        new SD('。', '.'),
        new SD('?', '?'),
    };

//全角转半角


    public static String sbc2dbc(String src)
    {
        if (src == null)
        {
            return src;
        }

        int i;
        char ch;
        int len = src.length();
        char[] chs = src.toCharArray();
        int sdCount = sdArray.length;
        StringBuffer sbResult = new StringBuffer(len);

        try
        {
            for (int k=0; k<len; k++)
            {
                ch = chs[k];

                for (i=0; i<sdCount; i++)
                {
                    if (sdArray[i].sbc == ch) //全角字符

                    {
                    sbResult.append(sdArray[i].dbc);
                    break;
                }
                }

                if (i == sdCount) //没有查到对应的全角字符

                {
                    sbResult.append(ch);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return sbResult.toString();
    }

//半角转全角


    public static String dbc2sbc(String src)
    {
        if (src == null)
        {
            return src;
        }

        int i;
        char ch;
        int len = src.length();
        char[] chs = src.toCharArray();
        int sdCount = sdArray.length;
        StringBuffer sbResult = new StringBuffer(len);

        try
        {
            for (int k=0; k<len; k++)
            {
                ch = chs[k];

                for (i=0; i<sdCount; i++)
                {
                    if (sdArray[i].dbc == ch) //半角字符

                    {
                    sbResult.append(sdArray[i].sbc);
                    break;
                }
                }

                if (i == sdCount) //没有查到对应的半角字符

                {
                    sbResult.append(ch);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return sbResult.toString();
    }

阅读(1986) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-09-22 10:44:13

好傻的做法。。。。。