java - 只输出中文, 包含中文标点符号
在 Java 中直接使用Unicode 转码时会按照UTF-16LE 的方式拆分,并加上 BOM。 如果采用 UTF-16 拆分,在 Java 中默认采用带有 BOM 的 UTF-16BE 拆分。
String a ="12dss显示,‘;()中文只";
StringBuffer b = new StringBuffer();
for(int i = 0;i<a.length();i++)
{
char t = a.charAt(i);
String str = String.valueOf(t);
if(str.getBytes().length ==2)
{
b.append(str); }
}
System.out.println(b);
结果: 显示‘;()中文只
java:获取字符串中第一个汉字和第一个汉字汉字标点符号的位置?
package tool; public class CopyCat
{
public static void main ( String[] args )
{
String string = "adf你.?的说法sdf";
String reg = "[\u4e00-\u9fa5]";
int index = -1;
if (string.matches (".*" + reg + ".*"))
{
index = string.split (reg)[0].length ();
}
System.out.println (index);
String regex = "[。,!?()《》……、:——【】;’”‘“]";
int ind = -1;
if (string.matches (".*" + regex + ".*"))
{
ind = string.split (regex)[0].length ();
}
System.out.println (ind);
}
}
常用汉字 的unicode 码范围是:\u4e00-\u9fa5,下面一个例子是把中英文文档中的汉字提取出来的简单例子:
public class DrawEnglish
{
private static String draw(String content)
{
StringBuffer english = new StringBuffer();
String regex = "[\u4e00-\u9fa5。,?”“《》:!——-、]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
while(matcher.find())
{
String temp = matcher.group();
english.append(temp);
}
return english.toString();
}
public static void drawEnglish(String path)
{
FileInputStream fr;
BufferedReader br;
FileWriter fw;
BufferedWriter bw = null ;
try
{
fr = new FileInputStream(path);
br = new BufferedReader(new InputStreamReader(fr,"gb2312"));
fw = new FileWriter("new1.txt");
bw = new BufferedWriter(fw);
String str = null;
StringBuffer sb = new StringBuffer();
while((str = br.readLine()) != null)
{
sb.append(str + "\n");
}
String temp = draw(sb.toString());
bw.write(temp);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(bw != null) bw.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
drawEnglish("draw1.txt");
}
}
java - 只输出中文, 包含中文标点符号的更多相关文章
- java - 只输出不含中文标点符号的中文
String a ="12dss显示,‘:()中文只"; StringBuffer b = new StringBuffer(); for(int i = 0;i<a.len ...
- Java检查字符串是否包含中文字符
转自:https://blog.csdn.net/zhanghan18333611647/article/details/80038629 强烈推荐一个大神的人工智能的教程:http://www.ca ...
- java - 只输出中文,(不包含标点)
String a ="12dss显示,‘:()中文只"; StringBuffer b = new StringBuffer(); for(int i = 0;i<a.len ...
- java 验证字符串是否包含中文字符
中文的模式:"[\\u4e00-\\u9fa5]|\\\\u" 例子: private static final Pattern p = Pattern.compile(" ...
- Java随机输出验证码包含数字、字母、汉字
//随机验证码,有数字.字符 //生成随机数,然后再截取,还要限定随机数的范围 String zimu = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn ...
- C#、Java实现按字节截取字符串包含中文汉字和英文字符数字标点符号等
C#.Java实现按字节截取字符串,字符串中包含中文汉字和英文字符数字标点符号等. 在实际项目应用过程中,尤其是在web开发时可能遇到的比较多,就以我的(JiYF笨小孩管理系统)为例,再发布文章时候, ...
- java判断字符串中是否包含中文 过滤中文
package com.test; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test ...
- java 判断字符串中是否包含中文并过滤掉中文
java判断字符串中是否包含中文并过滤掉中文 CreateTime--2017年9月6日08:48:59 Author:Marydon 1.判断字符串中是否包含中文方法封装 /** * 判断字符串 ...
- java - 输入的字符串中是否包含中文
今天和同事在讨论一个问题,需要检查“输入的字符串中是否包含中文”,刚开始想到是用正则表达式,正则表达式中是以[u4e00-u9fa5]来全匹配字符是否是中文,但现在面临的问题是这个字符串中还可能包含英 ...
随机推荐
- 四十五 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的bool组合查询
bool查询说明 filter:[],字段的过滤,不参与打分must:[],如果有多个查询,都必须满足[并且]should:[],如果有多个查询,满足一个或者多个都匹配[或者]must_not:[], ...
- C++多态、虚函数、纯虚函数、抽象类
多态 同一函数调用形式(调用形式形同)可以实现不同的操作(执行路径不同),就叫多态. 两种多态: (1)静态多态:分为函数重载和运算符重载,编译时系统就能决定调用哪个函数. (2)动态多态(简称多态) ...
- beego配置文件
关于App配置: #App配置 for Api AppName = ApiService RunMode = dev RouterCaseSensitive = true ServerName = A ...
- 头文件string.h中的函数及使用方法
来源:http://blog.csdn.net/tsyj810883979/article/details/5116817 字符串拷贝1 @函数名称: strdup函数原型: char *st ...
- sublime上配置markdown
等等等等 简书一个不错的教程:Sublime Text3的Markdown配置 补充说明:第一步可以直接找 Tools-->install package control. ^.^ ...
- [css小技巧]input去除边框问题
border:none;是不够的 (1)在谷歌浏览器添加 outline: none;去除点击后产生的边框; (2)IE7下border: none;还会有边框存在,改用border: 0;即可,同时 ...
- Java [Leetcode 383]Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from al ...
- Java集合总结之Collection整体框架
前段时间一直在忙一个物联网的项目,所以Java的学习一直搁置,从今天开始继续学习!望大家多提宝贵意见! java.util包中包含了一些在Java 2中新增加的最令人兴奋的增强功能:类集.一个类集(c ...
- QtAV的编译方法
1--编译准备 QtAV的安装编译总指导说明:https://github.com/wang-bin/QtAV/wiki/Build-QtAV QtAV的源代码:https://github.com/ ...
- FastAdmin 一键 CRUD 生成时方法不存在的问题分析
FastAdmin 一键 CRUD 生成时方法不存在的问题分析 有群友反馈 使用 一键 CRUD 生成时不成功. 我试了以下命令 php think crud -t test -u 1 是成功的. 再 ...