字符处理

1.字符串和数字之间的转换 从页面表单中取到的数据是字符串,而数据库中存储的是整型时

,就要用到字符串和数字之间的转换.在字符串和Integer之间转换,可

以使用Integer的函数Integer.parseInt(String)来转换

2.自动截取特定长度的字符串 在网页上显示数据的时候,往往有超长的数据,这样就会影响到整个页

面的布局,有没有好办法呢?其实,碰到这样的情况,可以用程序自动

处理一下,把超过某一固定长度的数据用省略号表示,模块如程序2所

示。

程序2 自动截取字符串的函数

/**

* 截取一定长度字符串,用于页面显示,如果长度超出截取部分,用

postfix续尾

*

* @param 原始字符串

* @param 开始位置

* @param 结束位置

* @param postfix 超长部分使用postfix参数结尾

* @return 返回转换后的字符串

*/

public static String substring(String str, int start, int end,

String postfix) {

int length = str.length();

if (length <= start) {

return str;

} else if (length <= end){

return str.substring(start, length);

} else {

return str.substring(start, end) + postfix;

}

}

3.切分字符串 如程序3 所示的代码完成了字符串的切分功能。在功能上类似于String

类的split方法。该函数使用给定的字符c把字符串切分成两部分,如果

字符串s不包含字符串c,则直接返回字符串。

程序3 切割字符串的函数

//把给定的字符串s用特定的字符c切割成两部分

public static String[] split(String s, char c){

String as[] = {s,""};

int i = s.indexOf(c);

if(i!=-1) {

as[0] = s.substring(0, i);

as[1] = s.substring(i+1);

}

return as;

}

4.char和bytes之间的转换 函数chars2bytes完成从char到bytes类型的转换功能,而bytes2chars

函数则是chars2bytes的逆过程,它负责把bytes转换成char类型。其实

现代码如程序4所示。

程序4 char和bytes之间的转换函数

//char转换成bytes类型

public static byte[] chars2bytes(char ac[]) {

byte ab[] = new byte[ac.length * 2];

int i= 0;

for(int j=0;j<ac.length;j++){

char c0 = ac[j];

char c1 = ac[j];

ab[i++] = (byte)(c0>>8);

ab[i++] = (byte)(c1);

}

return ab;

}

//bytes 转换成char类型

public static char[] bytes2chars(byte ab[])

throws Exception {

if(ab.length %2!=0)

throw new Exception("Can't connvert an odd number of

bytes");

char ac[] = new char[ab.length/2];

int i =0 ;

for(int j=0;j<ac.length;j++){

byte b0 = ab[i++];

byte b1 = ab[i++];

ac[j] = (char)(b0 << 8 & 0xff00 | b1 & 0xff);

}

return ac;

}

5.编码转换 使用正确的编码转换字符,可以让不同国家和地区的人,在浏览读者的

网页时候正常显示页面信息,而不出现乱码,如程序5所示。

程序5 编码转换功能模块

//把给定编码转换成Unicode

public static String toUnicode(String s, String lang) {

if (s == null) return s;

try {

byte[] target_byte = s.getBytes();

return new String(target_byte, lang); // "gb2312".

"ISO8859-1" ."GBK"..

} catch (Exception ex) {

return s;

}

}

//应用示例

public static String toUnicode(String s) {

return toUnicode(s,"ISO8859-1");

}

// 把Unicode转换成给定的编码

public static String toEncode(String s, String lang) {

if (s == null) return s;

try {

byte[] target_byte = s.getBytes(lang);

return new String(target_byte);

} catch (Exception ex) { return s; }

}

//应用示例

public static String toEncode(String s) {

return toEncode(s,"ISO8859-1");

}

6.编码特殊的HTML字符 为防止页面出错,尤其是在使用者可以提交数据到服务器的情况下,肯

定要用到这样的功能,防止自己的页面构架被别人“篡改”,如程序6

所示。

程序6 处理特殊字符,如'\n','<'等

public static String toHTML(String s)

{

char c[] = s.toCharArray();

char ch;

int i = 0;

StringBuffer buf = new StringBuffer();

while (i < c.length)

{

ch = c[i++];

if (ch == '"') buf.append("&quot;");

else if (ch == '&') buf.append("&amp;");

else if (ch == '<') buf.append("&lt;");

else if (ch == '>') buf.append("&gt;");

else if (ch == '\'') buf.append("&apos;");

else buf.append(ch);

}

return buf.toString();

}

7.格式化HTML代码 方便数据在JSP页面中显示,这就需要把“\n”转换成“<br>”,其代

码如程序7所示。

程序7 处理HTML回车和换行

public static String formatHTML(String s)

{

char c[] = s.toCharArray();

char ch;

int i = 0;

StringBuffer buf = new StringBuffer();

while (i < c.length)

{

ch = c[i++];

if (ch == '\n') buf.append("<br>");

else

buf.append(ch);

}

return buf.toString();

}

你可能感兴趣的:(C++,c,应用服务器,C#,J#)