获取字符串(代码格式)的函数

类别:Java 点击:0 评论:0 推荐:
在产生java代码文件时,我们常常会遇到要讲字java符串转换成代码格式.
比如回车要编程字符串"\n"等.
static String getEscapeString(String str) {         if (str == null) {             return null;         }         char[] bys = new char[str.length()];         str.getChars(0, str.length(), bys, 0);         StringBuffer ttsb = new StringBuffer();         for (int k = 0; k < bys.length; k++) {             switch (bys[k]) {                 case '\\':                 case '\'':                 case '\"':                     ttsb.append("\\");                     ttsb.append( (char) bys[k]);                     break;                 case '\r':                     ttsb.append("\\r");                     break;                 case '\n':                     ttsb.append("\\n");                     break;                 case '\t':                     ttsb.append("\\t");                     break;                 default:                     if (bys[k] < ' ' || bys[k] >= '\200') {                         //System.out.println("unicode...");                         ttsb.append("\\u" /*243*/);                         String s13 = Integer.toHexString(bys[k]);                         for (int l = s13.length(); l < 4; l++) {                             ttsb.append('0');                         }                         ttsb.append(s13);                     } else {                         ttsb.append( (char) bys[k]);                     }             }         }         return ttsb.toString();     }

本文地址:http://com.8s8s.com/it/it9614.htm