【今日】
寒冷的冬天没有雪,夏天的风也不会吹。
-----------星星再也不会亮
目录
一 String类
单个字符可以用char类型保存,多个字符组成的文本就需要保存在String对象中。
1.1声明字符串
在JAVA语言中,字符串必须包含在一对双引号(“”)之内。例如:
"23.23" "WDES" "你好"
声明一个字符串就是创建一个字符串对象,可以通过以下语法格式来声明字符串变量:
String str;
▢String:指定该变量为字符串类型。
▢str:任意有效的标识符,代表字符串变量的名称。
String str; 相当于 String str = null;
1.2创建字符串
创建字符串也可以理解为给字符串赋值。
主要方法有:
▢引用字符串常量
▢利用构造方法直接实例化
▢利用字符数组实例化
1.2.1引用字符串常量
字符串是常量,可以直接赋值给String。例如:
String a="我以为是规则,失去最爱的一个";
String b="才能记忆深刻",c="那些幼稚的 轻狂的 勇敢的";
String str1,str2;
str1="从此收着";
str2="我还在羡慕什么";
public class Demo {
public static void main(String[] args) {
String a="我以为是规则,失去最爱的一个";
String b="才能记忆深刻",c="那些幼稚的 轻狂的 勇敢的";
String str1,str2;
str1="从此收着";
str2="我还在羡慕什么";
System.out.print(a);
System.out.println(b);
System.out.print(c);
System.out.println(str1);
System.out.print(str2);
}
}
1.2.2 利用构造方法直接实例化
语法:public String(String original )
public class Demo {
public static void main(String[] args) {
String a= new String("《那是你离开北京的生活》");
String b=new String(a);
System.out.println(b);
}
}
1.2.3 利用字符数组实例化
方法一:
语法:构造方法:public String(char[] value )
public class Demo {
public static void main(String[] args) {
char[] Array= {'我','爱','你'};
String a=new String(Array);
System.out.println(a);
}
}
利用字符数组实例化的第二种用法:
【语法】 构造方法: public String(char[] value , int offset , int count )
▢value 字节数组
▢offset 起始位置
▢count 获取个数
例如
char[] charArray = { '时', '间', '就', '是', '金', '钱' }; String a = new String(charArray, 4, 2);
public class Demo {
public static void main(String[] args) {
char[] Array= {'不','是','我','也','不','是'};
String a=new String(Array,1,3);
System.out.println(a);
}
}
二 连接字符串
2.1连接多个字符串
使用“+”运算符可以实现连接多个字符串的功能。
public class Demo {
public static void main(String[] args) {
String a="太久没有写歌我不知道怎么起笔 ";
String b=" 寂寞的夜里我总是会想起你";
String c=a+b;
System.out.println(c);
}
}
也可以使用“+=”
public class Demo {
public static void main(String[] args) {
String a="123";
a+="abc";
System.out.println(a);
}
}
2.2连接其他类型数据
字符串也可同其他基本数据类型进行连接。如果将字符串同其他数据类型数据进行连接,会将其他数据类型的数据直接转换成字符串。
public class Demo {
public static void main(String[] args) {
int booktime = 4;
float practice = 2.5f;
System.out.println("我每天花费"+booktime+"小时看书;"+practice+"小时上机练习");
}
}
三 获取字符串信息
3.1获取字符串长度
使用String类的length()方法可以获取声明的字符对象的长度。语法如下:
str.length();
▢str为字符串对象。
获取字符串长度,代码如下:
String str = "we are students";
int size str.length();
public class Demo {
public static void main(String[] args) {
String a="123456";
int size=a.length();
System.out.println(size);
}
}
【注意】:
length()方法返回的字符串长度包括字符串中的空格。
3.2字符串查找
String类提供了两种查找字符串的方法。即indexOf()与lastIndexOf()。
indexOf()方法返回的 是搜索的字符或字符串首次出现的位置;
lastIndexOf()方法返回的是搜索的字符或字符串最后一次出现的位置。
1. indexOf(String s)
该方法用于返回参数字符串S在指定字符串中首次出现的索引位置。当调用String类的indexOf()方法时,会从当前字符串的开始位置搜索S的位置。如果没有检索到字符串S,该方法的返回值是-1;
public class Demo {
public static void main(String[] args) {
String a="we are good GGB";
int size =a.indexOf("GGB");
System.out.println(size);
}
}
返回的是字符串首次出现的位置12.
这里下标为什么是12呢?因为空格也算一位。
2.lastIndexOf(String str)
该方法用于返回指定字符串最后出现的索引位置。当调用String类的lastIndexOf()方法类时,会从当前字符串的开始位置检索参数字符串str,并将最一次出现str的检索位置返回。如果没有检索到字符串str,该方法返回-1.
public class Demo {
public static void main(String[] args) {
String a="we are good GGB";
int size =a.lastIndexOf("o");
System.out.println(size);
}
}
【注意】如果lastIndexOf()方法中的参数是空字符串""(注意没有空格),则返回的结果与调用length()方法返回的结果相同。
public class Demo {
public static void main(String[] args) {
String a="we are good GGB";
int size =a.lastIndexOf("");
System.out.println(size);
}
}
3.3获取指定索引位置的字符
使用charAt()方法可将指定索引位置处的字符返回。语法如下:
str.charAt(int index)
▢str:任意字符串。
▢index:整型值,用于指定要返回字符的下标。
public class Demo {
public static void main(String[] args) {
String a="我的男神是GGBond";
char b=a.charAt(3);
System.out.println(b);
}
}
【训练】判断文字出现的次数
设计一个方法,判断某段文字是否在指定字符串中只出现一次。
public class Demo {
public static void main(String[] args) {
String a="寒冷的冬天没有雪,夏天的风也不会吹。";
int size1=a.indexOf("天");
int size2=a.lastIndexOf("天");
if(size1!=size2) {
System.out.println("天字出现的次数大于1");
}else {
System.out.println("天字只出现了1次");
}
}
}
【训练6】获得最中间的字符
如何获得任意字符串里最中间的字符(如果字符个数为偶数,则取中间两个字符中索引值较小的字符)?
public class Demo {
public static void main(String[] args) {
String a="寒冷的冬天没有雪,夏天的风也不会吹。";
int size=a.length();
if(size%2==1) {//奇数
int c=size%2+1;
char a1=a.charAt(c);
System.out.println("字符串长度为奇数,最中间的字符为:"+a1);
}else if(size%2==0) {
int d=size/2;
char a2=a.charAt(d);
System.out.println("字符串长度为偶数,最中间的字符为:"+a2);
}
}
}