java 编程

[复制链接]
查看11 | 回复4 | 2010-2-20 14:21:57 | 显示全部楼层 |阅读模式
str = str.subString(0,8);system.out.println(str);应该行的
回复

使用道具 举报

千问 | 2010-2-20 14:21:57 | 显示全部楼层
取第一个汉字的ASCII码,然后截取到某个字符了,拿出这个字符的ASCII码跟前面的汉字ASCII码比较,如果大与等于那么取一下下一个,如果下一个还是大于等于就把这两个都删除掉留下前面的前面,如果下一个小于了,那么把前面的删除,不知道我的表达你能了解不,仅供参考
回复

使用道具 举报

千问 | 2010-2-20 14:21:57 | 显示全部楼层
public class Test { public static void main(String[] args) {String s = "我ABC汉DEF";System.out.println(Test.test(s, 4)); }public static String test(String str, int num) {return str.substring(0,num); }}这个不是WEB,在JAVA中一个中文还是一个长度,这样写就可以了
回复

使用道具 举报

千问 | 2010-2-20 14:21:57 | 显示全部楼层
……,有那么复杂吗?看看你问的问题先。编码格式不对才会出现这个效果吧。直接把字符串转换成UTF-8或者ISO-8859-1不就好了
回复

使用道具 举报

千问 | 2010-2-20 14:21:57 | 显示全部楼层
package com.ge.bean.csr;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Demo { /*** 判断是否为中文字符*/ public static boolean is (char mChar) {boolean is = false;String strTemp = String.valueOf(mChar);String strReg = "[\\u4e00-\\u9fa5]";Pattern p = Pattern.compile(strReg);Matcher m = p.matcher(strTemp);if (m.find()) { is = true;}return is; } public static String getString (String strDemo, int nBytes) {
char[] mChars = strDemo.toCharArray();int nDone = 0;for (int nIdx = 0; nDone < nBytes; nIdx ++) { if (is(mChars[nIdx])) {
nDone += 2; } else {
nDone += 1; }}byte[] mResChars = new byte[nDone];byte[] mBytes = strDemo.getBytes();System.arraycopy(mBytes, 0, mResChars, 0, nDone);return new String(mResChars); } public static void main (String[] args) {String str = "我a是中国人";int nBytes = 4;//输出我a是//int nBytes = 5;//输出我a是System.out.println(getString(str, nBytes)); }}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行