1.字符串判定是否含有某字符串
可以利用.contains
String temp = "abcde"
if(temp.contains("a")){
System.out.println("YES");
}
2.将字符串当中的某个字符替代为另一个字符
可以利用.replace
String temp ="abced";
temp=temp.replace("a","A");
System.out.println(temp);
temp=temp.replace("b","");
System.out.println(temp);
3.判断字符串是否为空
.isEmpty
String temp ="abced";
System.out.println(temp.isEmpty());
4.找字符串某个字符在该字符串的第几位
.indexOf
String temp ="abcedPPP";
nt s = temp.indexOf("P");
System.out.println(s);
这个方法不仅可以知道某个字符是第几个位置(从0开始数),也可以推出来,该字符前目有几个字符(不用减一),但是该方法只能显示最先探寻到的P
以上1-4总结 均可以在pat乙级1003应用到
5.BufferedReader用空格读取数据
使用Scanner 很容易做到 只要用next就好了,不要使用nextLine即可
但是用BufferedLine就可能头疼了,因为BufferedLine似乎在String当中没有read()方法
写出来是这样:
我们发现,连空格也都进去了,Line即为一行一行的读,此时要以空格 那么我们可以用split将其需要的分开,如果一行已知有多少空格,可以使用数组,未知可以使用arrayList;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String [] temp = new String [2];
temp = in.readLine().split(" ");
String a = temp[0];
String b = temp[1];
System.out.println(a);
System.out.println(b);
该方法可以在乙级1004及后面需要空格隔开读取数据的题目