在 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 - 只输出中文, 包含中文标点符号的更多相关文章

  1. java - 只输出不含中文标点符号的中文

    String a ="12dss显示,‘:()中文只"; StringBuffer b = new StringBuffer(); for(int i = 0;i<a.len ...

  2. Java检查字符串是否包含中文字符

    转自:https://blog.csdn.net/zhanghan18333611647/article/details/80038629 强烈推荐一个大神的人工智能的教程:http://www.ca ...

  3. java - 只输出中文,(不包含标点)

    String a ="12dss显示,‘:()中文只"; StringBuffer b = new StringBuffer(); for(int i = 0;i<a.len ...

  4. java 验证字符串是否包含中文字符

    中文的模式:"[\\u4e00-\\u9fa5]|\\\\u" 例子: private static final Pattern p = Pattern.compile(" ...

  5. Java随机输出验证码包含数字、字母、汉字

    //随机验证码,有数字.字符 //生成随机数,然后再截取,还要限定随机数的范围 String zimu = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn ...

  6. C#、Java实现按字节截取字符串包含中文汉字和英文字符数字标点符号等

    C#.Java实现按字节截取字符串,字符串中包含中文汉字和英文字符数字标点符号等. 在实际项目应用过程中,尤其是在web开发时可能遇到的比较多,就以我的(JiYF笨小孩管理系统)为例,再发布文章时候, ...

  7. java判断字符串中是否包含中文 过滤中文

    package com.test; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test ...

  8. java 判断字符串中是否包含中文并过滤掉中文

      java判断字符串中是否包含中文并过滤掉中文 CreateTime--2017年9月6日08:48:59 Author:Marydon 1.判断字符串中是否包含中文方法封装 /** * 判断字符串 ...

  9. java - 输入的字符串中是否包含中文

    今天和同事在讨论一个问题,需要检查“输入的字符串中是否包含中文”,刚开始想到是用正则表达式,正则表达式中是以[u4e00-u9fa5]来全匹配字符是否是中文,但现在面临的问题是这个字符串中还可能包含英 ...

随机推荐

  1. python中zip()函数的用法

    一. 定义 zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少内存 如果各个迭代器的元素个数不一致,则返回列表长度与最 ...

  2. Codeforces Round #451 (Div. 2)

    水题场.... 结果因为D题看错题意,B题手贱写残了...现场只出了A,C,E A:水题.. #include<bits/stdc++.h> #define fi first #defin ...

  3. cin关闭流同步加速

    习惯了用cin 很多人会说cin的速度比scanf慢很多, 其实不然. cin慢的原因主要在于默认cin与stdin总是保持同步, 这一步是消耗时间大户. 只需要加上std::iOS::sync_wi ...

  4. 017——VUE中v-fo指令的使用方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. centOS 7 tomcat nginx 验证码乱码

    将服务部署在centOS 7上,配置完tomcat和nginx之后,启动服务后,发现验证码这样了~~~ 一开始是以为nginx的原因,但是在Ubuntu系统相同操作发现没有问题,后发现,系统的字体库中 ...

  6. (效果二)js实现两个变量值的交换

    ES5: var a = 12,b=13,c; c = a; a = b; b = c; console.log(a,b);//13,12 通过设置第三方变量交换赋值来实现   ES6 var a = ...

  7. 使用iptables nat进行端口转发

    1.将发向HostA:PortA的请求转发到HostB:PortB iptables -t nat -A PREROUTING -p tcp -i eth0 -d HostA --dport Port ...

  8. NOIP模拟题 友好国度

    题目大意 给定一棵树,每个点有点权,求有多少组点对满足两点简单路径上的所有点点权的$gcd=1$. $n,val_i\leq 10^5$ 题解 考虑设$G_i$表示简单路径上所有点点权均为$i$的倍数 ...

  9. WPF XMAL获取元素的父元素,子元素

    /// 获得指定元素的父元素 /// </summary> /// <typeparam name="T">指定页面元素</typeparam> ...

  10. SCARA——OpenGL入门学习三

    OpenGL入门学习[三] 在第二课中,我们学习了如何绘制几何图形,但大家如果多写几个程序,就会发现其实还是有些郁闷之处.例如:点太小,难以看清楚:直线也太细,不舒服:或者想画虚线,但不知道方法只能用 ...