1.substring(beginindex,endindex) 截取字符串,从beginindex到endindex-1的字符串2.toLowerCase() 将整个字符串转化为小写字母
3.toUpperCase() 将整个字符串转化为大写字母
4.trim() 去掉整个字符串的首尾空格
5.replace() 去掉所有的空格,利用替换的思想
6.split(),以空白字符切割整个字符串,,并新建一个数组来存放它
代码:
String test=reader.nextLine();//接收文章
String []spString=test.split("\\s+");//以空白划分文章
7.反转字符串,这个是String类是没有的,要把字符串转成StringBuffer类,反转后再转回来。
//num为一个字符串
StringBuffer b = new StringBuffer(num);
num = b.reverse().toString();// 将该整数反转过来
String a=" asdfghjkldfghjkl ";
String b="ASDFGHGH";
System.out.println(a.substring(4,7));//截取字符串,从4到6(7-1)。
System.out.println(a.trim());//去掉首尾空格
System.out.println(a.toUpperCase());//将字符串转化为大写字母
System.out.println(b.toLowerCase());//将字符串转化为小写字母
String c=" a b c";
System.out.println(c.replace(" ", ""));//去掉所有的空格
//就是利用替换,把所有的空格都消除
System.out.println(c.replaceAll(" +",""));//去掉所有的空格
String d=" f g k k";
System.out.println(d.replaceAll("\\s*", ""));//去掉所有的空白字符串
//包括 tab、空格等这些, /s可以匹配大多数的空白字符
洛谷里面对于字符串的处理的题目:
https://www.luogu.com.cn/problem/P1308
https://www.luogu.com.cn/problem/P1603
https://www.luogu.com.cn/problem/P1071
https://www.luogu.com.cn/problem/P1553
这四道题如果能AC了,我觉得,最基础的对于字符串的处理方法已经有一点基础了。