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]来全匹配字符是否是中文,但现在面临的问题是这个字符串中还可能包含英 ...
随机推荐
- Ceph配置项动态变更机制浅析
转自:https://www.ustack.com/blog/ceph%e9%85%8d%e7%bd%ae%e9%a1%b9%e5%8a%a8%e6%80%81%e5%8f%98%e6%9b%b4%e ...
- OpenStack Mitaka HA部署方案(随笔)
[Toc] https://github.com/wanstack/AutoMitaka # 亲情奉献安装openstack HA脚本 使用python + shell,完成了基本的核心功能(纯二层的 ...
- Node.js小白开路(一)-- 全局变量篇
全局内容是有点类似于我们在浏览器编程的时候的window对象的,当时在node之中虽然我们编写的变量会自动的给出上下文环境(模块),但是全局变量的存在还是大大的帮助了我们编程时候的便捷性.我们可以在任 ...
- vs中: 错误,未定义的标识符getline 的解决方法
这种情况一般都是,在使用的时候没有include<string>而导致的,加上就可以正确编译通过
- LeetCode OJ :Remove Linked List Elements (移除链表元素)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 【python】命令行解析工具getopt用法
处理命令行参数的模块 用法: opts, args = getopt.getopt( sys.args[1:], shortStr, longList) 输入: shortStr 形式如下: &q ...
- Android(Lollipop/5.0) Material Design(一) 简介
官网地址:https://developer.android.com/intl/zh-tw/design/material/index.html 使用Material Design 需要api21,即 ...
- Python之contextlib库及源码分析
Utilities for with-statement contexts __all__ = ["contextmanager", "closing", &q ...
- fn project Message Queues 配置
Message Queues A message queue is used to coordinate asynchronous function calls that run through ...
- vuejs angularjs 框架的一些比较(vue项目重构四)
使用Angularjs和Vue.js对比 首先需要说明的是:现在默认angularjs指angular1.0+版本,angular默认指2.0以上版本.本文的名词也默认指定angular的1.0+版本 ...