Javastudy_day006
再探字符串
1、字符串比较大小
package com.day0001.contact;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String s;
int i=3;
//s1.compareTo(s2)
//如果s1比s2小,结果为负; 如果s1和s2相等,结果为0; 如果s1比s2大,结果为正
//s1.compareToIgnoreCase(s2) 不区分大小写的地比较大小
while(i!=0)
{
s=in.nextLine();
System.out.println("abcd".compareTo(s));
//System.out.println("ABCD".compareToIgnoreCase(s));
}
}
}
package com.day0001.contact;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String s;
int i=3;
//s1.compareTo(s2)
//如果s1比s2小,结果为负; 如果s1和s2相等,结果为0; 如果s1比s2大,结果为正
//s1.compareToIgnoreCase(s2) 不区分大小写的地比较大小
while(i!=0)
{
s=in.nextLine();
//System.out.println("abcd".compareTo(s));
System.out.println("ABCD".compareToIgnoreCase(s));
}
}
}
2、求得字符串长度
length()函数
package com.day0001.contact;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String name="Helloa";
String str1="one";
String str2="";
System.out.println(name.length()+" "+str1.length()+" "+str2.length());
//Error:String str3;str3.length();//因为str3没有管理任何String对象
}
}
3、访问字符串中某个位置的字符
package com.day0001.contact;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//访问String里的字符
String str1="helloworld";//0---9
System.out.println(str1.charAt(0)+" "+str1.charAt(1)+" "+str1.charAt(9));
//返回index上的单个字符;index的范围是0到length()-1
//但是不能用for-each循环来历遍字符串
}
}
4、求得子串
package com.day0001.contact;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//得到子串
String str1="helloworld";//0---9
System.out.println(str1.substring(5)+" "+str1.substring(0,5));
//s.substring(e) 从n号位置到末尾的全部内容
//s.substring(b,e) 得到从b号位置到e号位置之前的内容,即b到e-1
}
}
5、求得字符串中某个字符位置或子串的起始位置
package com.day0001.contact;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str1="helloworled";//0---10
int loc=str1.indexOf('e');//loc记录e字符第一次出现的位置
System.out.println(str1.indexOf('e')+" "+str1.indexOf('e',loc+1)+" "+str1.indexOf('o')+" "+str1.indexOf("worled"));
//s.indexOf('e')寻找字符e的第一个位置,如果没有找到返回-1
//s.indexOf('e',loc+1)从loc+1的位置开始找它后面的e的位置,如果找不到,返回-1
//s.indexOf("hello")寻找子串,返回子串的第一个字符在原串的位置,如果没有找到子串,返回-1
//indexOf()从左往右
//lastIndexOf()从右往左
System.out.println(str1.lastIndexOf('e')+" "+str1.lastIndexOf('e',loc+1)+" "+str1.lastIndexOf('o')+" "+str1.lastIndexOf("worled"));
}
}
6、其他String操作
s.startsWith(t) |
---|
判断s是不是以t开头的 |
s.endsWith(t) |
判断s是不是以t结尾的 |
s.trim() |
删掉字符串两端的空格 |
s.replace(c1,c2) |
把字符串中所有的c1都换成c2 |
s.toLowerCase() |
把整个字符串中都变成小写字母 |
s.toUpperCase() |
把字符串中都变成大写字母 |
所有的字符串都是不可变的,对他们的操作的结果都是制造新的字符串出来
package com.day0001.contact;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str1="HelloWorled";//0---10
String str2=str1.toUpperCase();
String str3=str1.toLowerCase();
String str4=str1.replace('W', 'A');
System.out.println(str1.replace('H', 'G')+" "+str4+" "+str1.toUpperCase()+" "+str2+" "+str3+" "+str1);
}
}
7、字符串switch-case
在java1.7版本及以上可以用字符串做switch-case
switch(s){
case "this": .....break;
case "that": .....break;
}