import java.util.HashMap;
import java.util.Iterator;
public class HomeWork {
public static void main(String[] args) {
String first ="I very happy";
/**
* 1将“I very happy”中的I替换成we are。
*/
String firstNew = first.replaceAll("I","we are" );
System.out.println(firstNew);
/**
* 2将“I very happy”中每个单词的最后一个字母转成大写。
*/
String [] ss = first.split(" ");
for(int i =0;i<ss.length;i++) {
String word = ss[i].substring(0,ss[i].length()-1)+String.valueOf(ss[i].charAt(ss[i].length()-1)).toUpperCase();
System.out.print(word+" ");
}
/**
*3 将“ hello world ”中的前后空格去掉
*/
String cc = " hello world ";
String three = cc.trim();
System.out.print("\n");
System.out.println(three);
/**
* 4将“I very happy”中所有字母变成大写。
*/
String four = first.toUpperCase();
System.out.println(four);
/**
* 5截取“I very happy”中的happy
*/
String fifth = first.substring(7);
System.out.println(fifth);
/**
* 6将“I study”和“nnxy college”拼接成一句话
*/
String aa = "I study";
String bb = "nnxy college";
System.out.println(aa+" "+bb);
/**
* 7判断字符串“you”在字符串“you are my friend.you are very smart.you work very hard.”中出现的次数。
*/
String dd="you are my friend.you are very smart.you work very hard";
int strlen = dd.length();
String afterstr = dd.replace("you", "");
int afterlen = afterstr.length();
int endlen = (strlen-afterlen)/3;
System.out.println("you在字符串中出现的次数为"+endlen);
/**
* 8、 请统计“you are my friend.you are very smart.you work very hard.”中,每个单词出现的次数。
*/
String ff ="you are my friend.you are very smart.you work very hard.";
String fff = ff.replace(".", " ");
HashMap<String, Integer> hm=new HashMap<>();
String [] strs=fff.split(" ");
for (int i = 0; i < strs.length; i++) {
if (!hm.containsKey(strs[i])) {
hm.put(strs[i], 1);
}else {
Integer counts=hm.get(strs[i]);
hm.put(strs[i], counts+1);
}
}
//遍历
Iterator<String> it=hm.keySet().iterator();
while(it.hasNext()) {
String keyName=it.next();
System.out.println(keyName+":"+hm.get(keyName)+"次");
}
}
}