trim() 函数只能去掉英文空格,如果是中文空格不知如何办了???急!
解决方案 »
- java中zlib压缩的问题
- 用JSP调用类的静态方法,该类的main是不是会自动执行?
- 请大虾们来看看,如何实现我正文的功能.关键字:JFreeChart,柱状图,CategoryDataset,ChartPanel,jDialog.
- java大师级进,望指点谜经
- 要将byte数组的内容用string输出,该怎么做?
- IOException问题
- 学了Java SE那么长时间,终于可以自己独立写东西了,Java实现万年历,鼓励现在才开始学Java的骚年
- [初学请教]
- 结构化文档全文索引的使用方式,以DB2 TIE为例,讲解一下结构化文档的全文索引的使用方法,希望对我的另一个帖子起到抛砖引玉的作用。
- 如何删除或者增加JTABLE中的行?
- 高分求教软件汉化的解决方法,回答有分
- 急!用javaapi测试自定义类的疑惑!高分相送
建议楼主再trim之前,用替换语句replaceAll,把全角空格替换成半角空格再处理~~~
public class EmptyLence {
public static void main(String[] args){
String a="我爱你abc啊啊啊啊 ";
String b="abc ";
System.out.println(a.trim());
System.out.println(a);
System.out.println(b.trim());
System.out.println(b);
}
}
public static void main(String[] args) {
String str ="tang ";
str = str.replaceAll(" "," ");
str= str.trim();
System.out.println(str.length());
}
}学习了
String strAAA = " ab ";
strAAA = strAAA.replaceAll("^[ | ](.*)[ | ]$","$1");
* @author chuayuan88
* 说明:该类实现把字符中的全角转化为半角,包括全角字符,全角空格.
* 在windows中,中文和全角字符都占两个字节,并且使用了ascii chart 2 (codes 128?C255)。
* 我们可以凭这一点来一个个检测用户输入的是否是中文和全角字符。
* 实际上,全角字符的第一个字节总是被置为163,而第二个字节则是相同半角字符码加上128(包 * 括空格)。
* 如半角a为65,则全角a则是163(第一个字节)、193(第二个字节,128+65)。 而对于中文来讲,*它的第一个字节被置为大于163,
* (如'阿'为:176 162),我们可以在检测到中文时不进行转换。
*/
public class EmptyLence { private static int b2i(byte n) {
int m = n;
if (n < 0) {
m = n + 256;
return m;
}
return m;
} private static byte i2b(int n) {
byte m = (byte) n;
if (n > 127) {
m = (byte) (n - 256);
return m;
}
return m;
} private static byte[] getOutputList(byte[] m) { //过滤byte数组中为0的元素
int len = m.length;
StringBuffer temp = new StringBuffer(1024);
for (int i = 0; i < len - 1; i++) {
if (m[i] != 0) {
temp.append(m[i]);
temp.append(",");
}
}
temp.append(m[len - 1]);
StringTokenizer st = new StringTokenizer(temp.toString(), ",");
int total = st.countTokens();
byte[] newList = new byte[total];
for (int i = 0; i < total; i++) {
newList[i] = (byte) Integer.parseInt(st.nextToken());
}
return newList; } public static void main(String[] args) {
String oldStr = "abc def 中国队";
int n = oldStr.getBytes().length;
byte[] strList = new byte[n + 1];
for (int j = 0; j < n; j++) {
strList[j] = oldStr.getBytes()[j];
}
byte[] newList = new byte[n];
int c1, c2;
for (int i = 0; i < newList.length; i++) {
c1 = b2i(strList[i]);
c2 = b2i(strList[i + 1]); if (c1 == 163) { //判断是否为全角字符
newList[i] = i2b(c2 - 128);
i++;
continue;
} if (c1 > 163) { //判断是否为汉字
newList[i] = i2b(c1);
newList[i + 1] = i2b(c2);
i++;
continue;
} if ((c1 == 161) && (c2 == 161))//全角空格是个特例,另加处理
{
newList[i] = 32;
i++;
continue;
}
newList[i] = i2b(c1); } byte[] outputList = getOutputList(newList);
System.out.println(new String(outputList));
}}
你的程序怎么编译过不去呢?jcreator下编译:
String oldStr = "锝侊絺锝冦??锝勶絽锝嗐??涓浗闃?";
^
1 errordos下编译:
C:\work>javac EmptyLence.java
EmptyLence.java:53: unclosed string literal
String oldStr = "锝侊絺锝冦??锝勶絽锝嗐??涓浗闃?";
^
1 error为什么呢?
String oldStr = "abc def 中国队";