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]来全匹配字符是否是中文,但现在面临的问题是这个字符串中还可能包含英 ...
随机推荐
- ultraedit使用记录
ultraedit使用记录 10:57:33 在日常的工作中,我经常用keil进行程序的编写等工作,不过在编写过程中Keil对中文的支持不是很好,容易发生问题:同事推荐我用ultraedit进行程序的 ...
- DBDB的维护和管理
挂载drbd前首先需要确认当前直接的DRDB分区是primary状态,可以从"cat /proc/drdb" 命令中查询节点状态.例如:[yongsan@mfsmaster drb ...
- DDOS工具合集---CC 2.0(僵尸网络proxy,单一url,可设置cookie,refer),传奇克星(代理+单一url,可设置cookie),NetBot_Attacker网络僵尸1.0(僵尸网络,HTTP NO-Cache Get攻击模式,CC攻击,HTTP空GET请求攻击),傀儡僵尸VIP1.4版(僵尸网络,动态单一url)、上兴网络僵尸2.3、中国制造网络僵尸、安全基地网络僵尸==
DDOS工具合集 from:https://blog.csdn.net/chinafe/article/details/74928587 CC 著名的DDOS CC工具,效果非常好!CC 2.0使用了 ...
- Cannot run program "svn" (in directory "G:\IntelliJ IDEA 2017.2.5"): CreateProcess error=2, 系统找不到指定的文件。
- Getting started with Android and Kotlin (译文)
原文链接 http://kotlinlang.org/docs/tutorials/kotlin-android.html 写在前面 Kotlin 是一个基于 JVM 的新的编程语言,由 JetBra ...
- Nginx 日志分析及性能排查
Nginx 日志分析及性能排查 2017-03-04 Linux爱好者 (点击上方公众号,可快速关注) 作者:-外星人- my.oschina.net/362228416/blog/844713 如有 ...
- PHPCMS V9调用父栏目 顶级父栏目的代码
一.调用父栏目 首先是列表页和二级栏目页list.html {$CATEGORYS[$top_parentid][catname]} //顶级父栏目名称 {$CATEGORYS[$CAT[parent ...
- iOS开发单例模式 dispatch_once
什么是单例 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源 ...
- matlab中求逆矩阵的高斯消元法实现的代码
function qiuni =INV_GET(a)N=length(a);M=eye(N);%得到上三角矩?for i=1:N max=a(i,i); A=i; for j=i+1:N if(abs ...
- CODEVS1049 棋盘染色
题目大意:01矩阵,1表示黑色,0表示白色,求将白色染成黑色最少的次数 使黑色成为一整个联通块. 题解: 搜索bfs 90... dfs判断连通 #include<iostream> #i ...