java去掉字符串中的中文,但要保留空格
String=“毛紫薇LagerstroemiavillosaWall.exKurz中国植物图像库”;想得到的结果是“LagerstroemiavillosaWall.e...
String=“毛紫薇 Lagerstroemia villosa Wall. ex Kurz 中国植物图像库”;
想得到的结果是“Lagerstroemia villosa Wall. ex Kurz” 该如何获得。 展开
想得到的结果是“Lagerstroemia villosa Wall. ex Kurz” 该如何获得。 展开
4个回答
展开全部
/**
* 判断中文字符
*
* @param oneChar
* @return true-是中文
* @author zhaigx
* @date 2013-7-11
*/
public static boolean checkCNChar(char oneChar) {
if ((oneChar >= '\u4e00' && oneChar <= '\u9fa5')
|| (oneChar >= '\uf900' && oneChar <= '\ufa2d'))
return true;
return false;
}
public static String deleteCNChar(String source) {
char[] cs = source.toCharArray();
int length= cs.length;
char [] buf = new char[length];
for (int i = 0; i <length; i++) {
char c = cs[i];
if (!checkCNChar(c)) {
buf[i] = c;
}
}
String ret = new String(buf);
return ret.trim();
}
public static void main(String[] args) {
String string = "毛紫薇 Lagerstroemia villosa Wall. ex Kurz 中国植物图像库";
string = deleteCNChar(string);
System.out.println(string);
}
展开全部
public static void main(String[] args) {
String string = "毛紫薇 Lagerstroemia villosa Wall. ex Kurz 中国植物图像库";
string = string.replaceAll("(\\s[\u4E00-\u9FA5]+)|([\u4E00-\u9FA5]+\\s)", "");
System.out.println("[" + string + "]");
}
[Lagerstroemia villosa Wall. ex Kurz]
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
// 字符操作
public class StringOperator {
public static void main(String[] args) {
// String=“毛紫薇 Lagerstroemia villosa Wall. ex Kurz 中国植物图像库”;
// 想得到的结果是“Lagerstroemia villosa Wall. ex Kurz” 该如何获得。
String srcString = "毛紫薇 Lagerstroemia villosa Wall. ex Kurz 中国植物图像库";
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < srcString.length(); i++) {
char c = srcString.charAt(i);
if (!isChinese(c)) {
buffer.append(c);
}
}
// 打印得到的字符串,但要注意,如果你要去掉字符串两端的空格,加一句 buffer.trim();.
System.out.println(buffer);
}
// 根据Unicode编码完美的判断中文汉字和符号
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
}
return false;
}
}
public class StringOperator {
public static void main(String[] args) {
// String=“毛紫薇 Lagerstroemia villosa Wall. ex Kurz 中国植物图像库”;
// 想得到的结果是“Lagerstroemia villosa Wall. ex Kurz” 该如何获得。
String srcString = "毛紫薇 Lagerstroemia villosa Wall. ex Kurz 中国植物图像库";
StringBuffer buffer = new StringBuffer("");
for (int i = 0; i < srcString.length(); i++) {
char c = srcString.charAt(i);
if (!isChinese(c)) {
buffer.append(c);
}
}
// 打印得到的字符串,但要注意,如果你要去掉字符串两端的空格,加一句 buffer.trim();.
System.out.println(buffer);
}
// 根据Unicode编码完美的判断中文汉字和符号
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
}
return false;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/**
*
*
*去汉字
*/
private static String countChar(String itemlife)
{
String regEx = "[\\u4e00-\\u9fa5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(itemlife);
return m.replaceAll("");
}
*
*
*去汉字
*/
private static String countChar(String itemlife)
{
String regEx = "[\\u4e00-\\u9fa5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(itemlife);
return m.replaceAll("");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询