String 类
什么是String 类:
String 类代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。
字符串是常量;它们的值在创建之后不能更改。
String()
初始化一个新创建的 String 对象,使其表示一个空字符序列。
String 类常见构造方法:
public String():
空构造
public String(String original):
把字符串常量值转成字符串
public class MyTest2 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);//false
System.out.println(s1.equals(s2));//true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4); //false
System.out.println(s3.equals(s4));//true
String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);//true
System.out.println(s5.equals(s6)); //true
}
}
public String( byte[] bytes):
把字节数组转成字符串
//定义了一个字节数组
byte[] bytes = {97, 98, 99, 100};
//把一个字节数组转换成字符串
String s = new String(bytes);
public String( byte[] bytes, int index, int length):
把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度);
//定义了一个字节数组
byte[] bytes = {97, 98, 99, 100};
//把一个字节数组转换成字符串
String s = new String(bytes);
//转换字节数组中的一部分字节,参2 起始索引,参数3 转换几个字节
s = new String(bytes, 1, 2);
System.out.println(s);
public String( char[] value):
把字符数组转成字符串
char[] chs = {'a', 'b', 'c', 'd', 'e'}; //"abcde";
String s1 = new String(chs);
public String( char[] value, int index, int count):
把字符数组的一部分转成字符串
char[] chs = {'a', 'b', 'c', 'd', 'e'}; //"abcde";
String s1 = new String(chs);
//把字符数组的一部分,转换成字符串 参2 起始索引,参数3 转几个字符
s1 = new String(chs, 0, 3);
System.out.println(s1);
练习程序
public class MyTest {
public static void main(String[] args) {
//定义了一个字节数组
byte[] bytes = {97, 98, 99, 100};
//把一个字节数组转换成字符串
String s = new String(bytes);
//转换字节数组中的一部分字节,参2 起始索引,参数3 转换几个字节
s = new String(bytes, 1, 2);
System.out.println(s);
System.out.println("--------------");
char[] chs = {'a', 'b', 'c', 'd', 'e'}; //"abcde";
String s1 = new String(chs);
//把字符数组的一部分,转换成字符串 参2 起始索引,参数3 转几个字符
s1 = new String(chs, 0, 3);
System.out.println(s1);
}
}
String类的常用方法:
String类的判断方法:
public boolean equals (Object obj):
比较字符串的内容是否相同, 区分大小写
boolean b = "abc".equals("abC"); //区分大小写的比较
public boolean equalsIgnoreCase (String str):
比较字符串的内容是否相同, 忽略大小写
System.out.println("abc".equalsIgnoreCase("ABC")); //不区分大小写的比较
public boolean contains (String str):
判断字符串中是否包含传递进来的字符串
boolean b1 = "刮风这天,我试过握着你手".contains("刮风这天");
public boolean startsWith (String str):
判断字符串是否以传递进来的字符串开头
System.out.println("刮风这天,我试过握着你手".startsWith("刮风这天"));
public boolean endsWith (String str):
判断字符串是否以传递进来的字符串结尾
System.out.println("刮风这天,我试过握着你手".endsWith("我试过握着你手"));
练习程序
public class MyTest {
public static void main(String[] args) {
boolean b1 = "七月的风,八月的雨".contains("八月的雨");
System.out.println(b1);
System.out.println("七月的风,八月的雨".startsWith("七月的风"));
System.out.println("七月的风,八月的雨".endsWith("八月的雨"));
//
String s="";//定义一个空串
boolean empty = s.isEmpty();
System.out.println(empty);
if(s.length()==0){
System.out.println("是空串");
}else{
System.out.println("不是空串");
}
//null 和 ""
//String s2=null;
//
//String s3="";
//
//s3.length();
//s2.length();
}
}
String类的获取方法:
public int length ():
获取字符串的长度。
System.out.println("abcd".length());
public char charAt ( int index):
通过索引获取出字符中的一个字符
char ch = "红尘来去梦一场".charAt(4);
public int indexOf ():
返回指定字符串在此字符串中第一次出现处的索引。
//查找该字符,在这个字符串中第一次出现的索引,从前往后找,找到就停止
int index = "红尘来去梦一场,红尘来去梦一场".indexOf('梦');
System.out.println(index);
public int lastIndexOf ():
String s="红尘来去梦一场,红尘来去和梦一场";
int index1 = s.lastIndexOf('梦');
System.out.println(index1);
例:怎么判断一个字符,在字符串中只出现一次
if(s.indexOf('和')==s.lastIndexOf('和')){
System.out.println("和字出现过一次");
}
//如果没有找到我要的字符串的索引,返回 -1 我们经常根据 -1 这个返回值来做一些判断
int index2 = "红尘来去梦一场,红尘来去和梦一场".indexOf("梦场");
System.out.println(index2);
//可以从指定索引除,检索,此字符,在字符串中,第一次出现的索引
int index3 = "红尘来去梦一场,红尘来去梦一场".indexOf('梦', 6);
System.out.println(index3);
String s2="红尘来呀来,去呀去,都是梦一场";
String substring = s2.substring(s2.indexOf("都"));
System.out.println(substring);
String s1 = s2.substring(6,9); //含头,不含尾
System.out.println(s1);
练习程序:
public class MyTest {
public static void main(String[] args) {
// 案例演示String类的获取功能
// "abcd" 字符串有索引,从0开始,也有长度
System.out.println("abcd".length());
//public char charAt ( int index):获取指定索引位置的字符
//通过索引获取出字符中的一个字符
char ch = "红尘来去梦一场".charAt(4);
System.out.println(ch);
//查找该字符,在这个字符串中第一次出现的索引,从前往后找,找到就停止
int index = "红尘来去梦一场,红尘来去梦一场".indexOf('梦');
System.out.println(index);
//从后往前找,该字符在字符串中第一次出现的索引
String s="红尘来去梦一场,红尘来去和梦一场";
int index1 = s.lastIndexOf('梦');
System.out.println(index1);
// 怎么判断一个字符,在字符串中只出现一次
if(s.indexOf('和')==s.lastIndexOf('和')){
System.out.println("和字出现过一次");
}
//如果没有找到我要的字符串的索引,返回 -1 我们经常根据 -1 这个返回值来做一些判断
int index2 = "红尘来去梦一场,红尘来去和梦一场".indexOf("梦场");
System.out.println(index2);
//可以从指定索引除,检索,此字符,在字符串中,第一次出现的索引
int index3 = "红尘来去梦一场,红尘来去梦一场".indexOf('梦', 6);
System.out.println(index3);
String s2="红尘来呀来,去呀去,都是梦一场";
String substring = s2.substring(s2.indexOf("都"));
System.out.println(substring);
String s1 = s2.substring(6,9); //含头,不含尾
System.out.println(s1);
}
}
例:统计字符串中大写字母,小写字母,数字的个数
public class MyTest3 {
public static void main(String[] args) {
//统计字符串中大写字母,小写字母,数字的个数
String s = "AbcdeFdfdgDKief10019FDFD002323343";
//定义统计变量
int xiao = 0;
int da = 0;
int num = 0;
for (int i = 0; i < s.length(); i++) {
//截取每一个字符
char ch = s.charAt(i);
if (ch >= 'a' && ch <= 'z') {
xiao++;
} else if (ch >= 'A' && ch <= 'Z') {
da++;
} else if (ch >= '0' && ch <= '9') {
num++;
}
}
System.out.println("小写字母有" + xiao + "个");
System.out.println("大写字母有" + da + "个");
System.out.println("数字有" + num + "个");
}
}