/*2014年4月10日 by shmilyxdl*/
package shen;
import java.util.*;
import java.util.Map.Entry;
class Sumword {
private int count = 1;
public Sumword(String temp) {
// 预处理
char data[] = temp.toCharArray(); // 字符串变为字符数组
for (int x = 0; x < data.length; x++) {
if ((0 > data[x]) || (127 < data[x])) {
for (int j = x; j < data.length - 1; j++)
data[j] = data[j + 1];
x--;
this.count++;
}
}
char[] data2 = new char[data.length - this.count];
System.arraycopy(data, 0, data2, 0, data.length - this.count);
// 以上除去可恶的汉字
String str2 = new String(data2);
int wordCount = 0; // 用于统计单词的总个数
Map<String, Integer> map = new HashMap<String, Integer>();// 用于统计各个单词的个数,排序
StringTokenizer token = new StringTokenizer(str2); // 这个类会将字符串分解成一个个的标记
while (token.hasMoreTokens()) { // 循环遍历
wordCount++;
String word = token.nextToken(", ?.!:\"\"‘‘\n<>1234567890");
if (map.containsKey(word)) { // HashMap不允许重复的key,所以利用这个特性,去统计单词的个数
int count = map.get(word);
map.put(word, count + 1); // 如果HashMap已有这个单词,则设置它的数量加1
} else
map.put(word, 1); // 如果没有这个单词,则新填入,数量为1
}
System.out.println(wordCount);
sort(map); // 调用排序的方法,排序并输出!
}
public static void sort(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
}); // 排序
System.out.println("【分别为】:");
for (int i = 0; i < infoIds.size(); i++) { // 输出
Entry<String, Integer> id = infoIds.get(i);
System.out.print(id.getKey() + ":" + id.getValue() + " ");
}
}
}
class Section {
public Section(String temp) {
String result[] = temp.split("\\,");
for (int x = 0; x < result.length; x++) {
System.out.println(result[x]);
}
}
}
public class Stringtest {
public static void main(String[] agrs) {
String str = "1.让清晨的阳光,洒在你熟睡的脸颊上,"+
"Let the morning sunshine in your sleeping on the cheek,"+
"2.让时间的流逝,悄悄印刻在你的眉梢,"+
"Let time slip engraved on your brow,"+
"3.让岁月的积淀,绽放出始终如一的绚丽爱情。"+
"For years the accumulation of blossom a consistent love.";
System.out.println(str);
System.out.println((str.getBytes().length == str.length()) ? "【****s1无汉字****】": "【****s1有汉字****】");
System.out.println("【str是否包含数字】:" + isNumber(str));
System.out.println("【str内包含字母的都全变成大写】:" + transCapital(str));// public String toUpperCase()
System.out.println("【str内包含字母的都全变成小写】:" + transLowercase(str));// public String toLowerCase()
System.out.println("【str的字母统计】:" + sumLetters(str));// 统计字母
System.out.println("【str的单词统计】:");
System.out.println(new Sumword(str));
System.out.println("【分段如下:】:");
System.out.println(new Section(str));
}
public static boolean isNumber(String temp) {
char data[] = temp.toCharArray(); // 变为字符数组
for (int x = 0; x < data.length; x++) {
if (data[x] > ‘0‘ || data[x] < ‘9‘) {
return true; // 不是数字
}
}
return false;
}
public static String transCapital(String temp) {
char data[] = temp.toCharArray(); // 字符串变为字符数组
for (int x = 0; x < data.length; x++) {
if (‘a‘ < data[x] && ‘z‘ > data[x])
data[x] -= 32; // 变大写
}
return new String(data);
}
public static String transLowercase(String temp) {
char data[] = temp.toCharArray(); // 字符串变为字符数组
for (int x = 0; x < data.length; x++) {
if (‘A‘ < data[x] && ‘Z‘ > data[x])
data[x] += 32; // 变小写
}
return new String(data);
}
public static int sumLetters(String temp) {
char data[] = temp.toCharArray();
int count = 0;
for (int x = 0; x < data.length; x++) {
if (‘A‘ < data[x] && ‘z‘ > data[x])
count++;
}
return count;
}
}本文出自 “此心未泯” 博客,谢绝转载!
java 实验五 String 常方法,布布扣,bubuko.com
原文:http://xudaolong.blog.51cto.com/4373607/1393632