【java】字符串中各种类型的字符串统计
2013-06-28 09:34
337 查看
package com.taylor.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ei {
public static void main(String[] args) {
String s = "你好123 4Aa Zz-+&^%";
Ei e = new Ei();
System.out.println(e.getCharCount(s));
System.out.println(e.getIntCount(s));
System.out.println(e.getSpaceCount(s));
System.out.println(e.getOtherCount(s));
}
//中英文字符
private int getCharCount(String s){
Pattern pat = Pattern.compile("[\u4E00-\u9FA5A-Za-z]",
Pattern.CASE_INSENSITIVE);
int i = 0;
Matcher mat = pat.matcher(s);
while (mat.find()) {
i++;
}
return i;
}
//数字
private int getIntCount(String s){
Pattern pat = Pattern.compile("[0-9]",
Pattern.CASE_INSENSITIVE);
int i = 0;
Matcher mat = pat.matcher(s);
while (mat.find()) {
i++;
}
return i;
}
//空格
private int getSpaceCount(String s){
Pattern pat = Pattern.compile("\\s",Pattern.CASE_INSENSITIVE);
int i = 0;
Matcher mat = pat.matcher(s);
while (mat.find()) {
i++;
}
return i;
}
//其他
private int getOtherCount(String s){
Pattern pat = Pattern.compile("[^\u4E00-\u9FA5A-Za-z0-9\\s]",
Pattern.CASE_INSENSITIVE);
int i = 0;
Matcher mat = pat.matcher(s);
while (mat.find()) {
i++;
}
return i;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ei {
public static void main(String[] args) {
String s = "你好123 4Aa Zz-+&^%";
Ei e = new Ei();
System.out.println(e.getCharCount(s));
System.out.println(e.getIntCount(s));
System.out.println(e.getSpaceCount(s));
System.out.println(e.getOtherCount(s));
}
//中英文字符
private int getCharCount(String s){
Pattern pat = Pattern.compile("[\u4E00-\u9FA5A-Za-z]",
Pattern.CASE_INSENSITIVE);
int i = 0;
Matcher mat = pat.matcher(s);
while (mat.find()) {
i++;
}
return i;
}
//数字
private int getIntCount(String s){
Pattern pat = Pattern.compile("[0-9]",
Pattern.CASE_INSENSITIVE);
int i = 0;
Matcher mat = pat.matcher(s);
while (mat.find()) {
i++;
}
return i;
}
//空格
private int getSpaceCount(String s){
Pattern pat = Pattern.compile("\\s",Pattern.CASE_INSENSITIVE);
int i = 0;
Matcher mat = pat.matcher(s);
while (mat.find()) {
i++;
}
return i;
}
//其他
private int getOtherCount(String s){
Pattern pat = Pattern.compile("[^\u4E00-\u9FA5A-Za-z0-9\\s]",
Pattern.CASE_INSENSITIVE);
int i = 0;
Matcher mat = pat.matcher(s);
while (mat.find()) {
i++;
}
return i;
}
}
相关文章推荐
- 【Java】三种方式实现统计字符串中字母,数字,空格和其它类型的数量
- Java统计字符串中各种字母个数
- 利用函数指针统计字符串中各种类型字符的个数
- fastjson 使用 string字符串转换成java的各种类型
- java学习,从一个字符串中统计同一类型出现的次数
- Java基础知识强化69:基本类型包装类之Character案例(统计字符串中大写小写以及数字的次数)
- 初学java:统计字符串中各种字符个数
- java各种数字类型与字符串的转换
- 统计某java文件中的代码行数、某个字符(char)、某字符串(String)出现的次数
- Java中基本类型和字符串的转换
- java统计字符串中字符及子字符串个数
- java字符串的各种编码转换
- JAVA-各种类型之间转换 parse() 与valueOf()
- java字符串类型时间转Date类型时间
- java中对字符串每个字符统计
- java 统计字符串中子字符串个数
- java统计字符串中出现最多的字符
- java 各种类型数据,集合对象封装 成json
- java数据类型 变量,运算符,字符串,输入输出,数组,大数值
- (JAVA初级) 字符串和基本类型的转换