String类
String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例
类String中包括了用于检查各个字符串的方法,比如:比较字符串,搜索字符串,提取字符串以及创建具有翻译为大写或者小写的所有字符的字符串的副本。
特点:
1、字符串不变:字符串的值在创建后不能改变
public class Demo {
public static void main(String[] args) {
String str="abc";
str+="d";
System.out.println(str);
}
}
//内存中有abc,abcd两个对象,刚开始str是指向abc
//改变指向,指向abcd
2、因为String对象不可以改变,所以它可以被共享
public class Demo {
public static void main(String[] args) {
String str="abc";
String str1="abc";
System.out.println(str==str1);
//内存中只有一个“abc",同时被str和str1共享
}
}
3、”abc” 等效于 char[] data={‘a’,’b’,’c’};
String str=”abc”;
等效于:
char[] data={‘a’,’b’,’c’};
String str=new String(data);
//String底层就是靠字符数组实现的
使用步骤
查看类java.lang.String:此类不需要导包
查看一下字符串的构造方法:
public String();:初始化新创建string对象,以使其表示空字符序列
public String(char[] ch):通过当前的参数中的字符数组来构建字符串
public String(byte[] byte):通过使用平台默认的字符解码当前的参数中的字节数组构建字符串
public class test03 {
public static void main(String[] args) {
//通过字符串的无参构造来构建一个空序列
String str=new String();
System.out.println(str);
//通过字符数组来构建字符串
char[] ch={'a','b','c'};
String str1=new String(ch);
System.out.println(str1);
//通过字节数组来构造
byte[] bytes={97,98,99};
String str2=new String(bytes);
System.out.println(str2);
}
}
常用比较方法:
public boolean equals(Object o):将此字符串与指定的对象进行比较
public boolean equalsIngoreCase(Object o):将此字符串与指定对象进行比较,忽略大小写
public class test04 {
public static void main(String[] args) {
String str="hello";
String str2="hello";
String str3="HELLO";
//public boolean equals():直接比较内容
System.out.println(str.equals(str2));
System.out.println(str.equals(str3));
//public boolean equalsIgnoreCase():忽略大小写进行比较
System.out.println(str.equalsIgnoreCase(str3));
}
}
获取功能的方法:
public int length(); 返回此字符串的长度
public String concat(String str):将指定的字符串拼接到该字符串的尾部
public char charAt(int index):返回指定索引处的char值
public int indexOf(String str):返回指定子字符串第一次出现在该字符串内的索引
public String subString(int beginIndex);返回一个子字符串,从beginIndex开始截取字符串到字符串的尾部
public String subString(int begin,int end);返回一个子字符串,从begin到end截取字符串,包含begin,但是不包含end
public class test05 {
public static void main(String[] args) {
//创建字符串的对象
String str="holleword";
//int length();返回此字符串的长度
System.out.println(str.length());
//String concat(String str);将指定的字符串拼接到该字符串的尾部
String str1=str.concat("aaa");
System.out.println(str1);
//返回指定索引处的char值
System.out.println(str.charAt(0));
System.out.println(str.charAt(1));
//返回指定子字符串第一次出现在该字符串内的索引
System.out.println(str.indexOf("o"));
//返回一个子字符串,从beginIndex开始截取字符串到字符串的尾部
System.out.println(str.substring(4));
//返回一个子字符串,从begin到end截取字符串,包含begin,但是不包含end
System.out.println(str.substring(2,4));
}
}
/*9
hollewordaaa
h
o
1
eword
ll*/
转换功能的方法:
pubic char[] toCharArray(); 将此字符串转换为字符数组
public byte[] getBytes();使用平台默认编码字符集将String编码转为新的字节数组
public class test10 {
public static void main(String[] args) {
//创建一个字符串对象
String str="abcde";
//char[] toCharArray();将字符串转换为字符数组
char[] chars=str.toCharArray();
for (int i = 0; i <chars.length ; i++) {
System.out.println(chars[i]);
}
System.out.println("---------");
//byte[] getBytes();使用平台默认字符集将String编码转为新的字节数组
byte[] bytes=str.getBytes();
for (int i = 0; i <bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("---------");
//public String replace(CharSequence target,CharSequence replacement)
String str1="buchishu";
String replace =str1.replace("bu","BU");
System.out.println(replace);
}
}
习题
1、定义一个方法,把数组[1,2,3]按照指定的格式拼接成一个字符串,格式例如:
[元素#元素#元素]
public class test06 {
public static void main(String[] args) {
int[] arr={1,2,3};
String string = arrayToString(arr);
System.out.println(string);
}
/*两个明确
* 返回类型 :String
* 参数列表:int[]
*/
public static String arrayToString(int[] arr){
//创建一个字符串
String s=new String("[");
//遍历数组,并拼接字符串
for (int i = 0; i <arr.length ; i++) {
if (i==(arr.length-1)) {
s = s.concat(arr[i] + "]");
}else{
s=s.concat(arr[i]+"#");
}
}
return s;
}
}
2、键盘录入一串字符串,统计字符串中的大小写字母的个数以及数字字符个数。
import java.util.Scanner;
public class test07 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个字符串数据:");
String str =scanner.nextLine();
//定义三个统计变量,初始值都是0
int bigConut=0;
int smallCount=0;
int numberCount=0;
//遍历字符串
for (int i = 0; i <str.length() ; i++) {
char c=str.charAt(i);
//拿字符直接判断
if (c>='A'&&c<='Z'){
bigConut++;
}else if (c>='a'&&c<='z'){
smallCount++;
}else if (c>='0'&&c<='9'){
numberCount++;
}else {
System.out.println("该字符串"+c+"是一个非法字符!");
}
}
System.out.println("大写字母的个数:"+bigConut);
System.out.println("小写字母的个数:"+smallCount);
System.out.println("数字的个数:"+numberCount);
}
}