1.String类
- 1.1声明字符串
String a;
String a,b;
在不给字符串变量赋值的情况下,默认值为null(空),就是空对象,如果此时调用String的方法会发生空指针异常。
- 1.2创建字符串
String a="时间就是金钱,我的朋友。";
String b="锄禾日当午",c="小鸡炖蘑菇";
String str1,str2;
str1="We are students";
str2="We are students";
其他几种方法
利用构造方法实例化
String a=new String("我爱清汤小肥羊");
String b=new String(a);
//利用字符数组实例化
char[] charArray=('t','i','m','e');
String a=new String(charArray);
//提取字符数组中的一部分创建字符串对象
char[] charArray=('时','间','就','是','金','钱');
String a=new String(charArray,3,2);
2连接字符串
- 2.1连接字符串
public static void main(String[] args) {
String a="abc";//定义一个字符串
String b="123";//定义一个字符串
String c=a+b+"!";//使用"+"拼接字符串
String d="拼接字符串";//定义一个字符串
d+=c;//使用"+="拼接字符串
System.out.println("a="+a);//输出信息
System.out.println("b="+b);//输出信息
System.out.println("c="+c);//输出信息
System.out.println("d="+d);//输出信息
}
java中相连的字符串 :分两行写需要加号连接
- 2.2连接其他数据类型
public static void main(String[] args) {
int booktime=4;//声明int型变量booktime
float practice=2.5f;//声明float型变量practice
System.out.println("我每天花费"+booktime+"小时看书;"+practice+"小时上机练习");//输出信息
}
+ 的一端是字符串那么他会将另一端转换为字符串
3.提取字符串信息
- 3.1获取字符串长度
length 关键字
使用:字符串.关键字
- 3.2获取指定的字符
charAt(index) 关键字
public static void main(String[] args) {
String str="床前明月光,疑是地上霜。";//定义一个字符串
char chr=str.charAt(4);//将字符串str中索引位置为4的字符赋值给chr
System.out.println("字符串中索引位置为4的字符是:"+chr);//输出chr
}
- 3.3获取子字符串索引位置
格式:a.indexOf(substr);
a:任意字符串对象
substr:要搜索的字符串。
public static void main(String[] args) {
String str="12345abcde";//创建字符串对象
int charIndex=str.indexOf("abc");//获取字符串str中"abc"首次出现的索引,赋值给charIndex
if(charIndex!=-1){//判断:index的值不等于-1
System.out.println("str中存在abc字符串");//如果index的值不等于-1,就执行此代码
}else{
System.out.println("str中没有abc字符串");//如果index的值等于-1,就执行此代码
}
}
如果参数是一个字符串,返回的结果是字符串第一个字母所在位置
indexOf(String str,int fromlndex)
从指定的索引fromIndex 开始至字符串最后,返回指定子字符串在此字符串中第一次出现处的索引。如果没有检索到字符串str,该方法的返回值为-1.
a.indexOf(str,fromIndex);
a:任意字符串对象
str:要搜索的子字符串
fromIndex:开始搜索的索引位置
public static void main(String[] args) {
String str="We are the world";//创建字符串
int firstIndex=str.indexOf("r");//获取字符串中"r"第一次出现的索引位置
int secondIndex=str.indexOf("r",firstIndex+1);//获取字符串中"r"第二次出现的索引位置
int thirdIndex=str.indexOf("r",secondIndex+1);//获取字符串中"r"第三次出现的索引位置
System.out.println("r第一次出现的索引位置是:"+firstIndex);//输出信息
System.out.println("r第一次出现的索引位置是:"+secondIndex);//输出信息
System.out.println("r第一次出现的索引位置是:"+thirdIndex);//输出信息
}
public int lastIndexOf(String str)
a.lastIndexOf(str);
a:任意字符串
str:要搜索的字符串
public static void main(String[] args) {
String str="Let it go!Let it go!";//创建字符串对象
int gIndex=str.lastIndexOf("g");//返回“g”最后一次出现的位置
int goIndex=str.lastIndexOf("go");//返回“go”最后一次出现的位置
int oIndex=str.lastIndexOf("o");//返回“o”最后一次出现的位置
System.out.println("字符串\"Let it go!Let it go!\"中:\n");//输出信息
System.out.println("\"g\"最后一次出现的位置是:"+gIndex);//输出信息
System.out.println("\"o\"最后一次出现的位置是:"+oIndex);//输出信息
System.out.println("\"go\"最后一次出现的位置是:"+goIndex);//输出信息
}
lastIndexOf(str,fromIndex)
a.lastIndexOf(str,fromIndex);
a:任意字符串
str:要搜索的子字符串
fromIndex:开始搜索的索引位置
public static void main(String[] args) {
String str="01a3a56a89";//定义一个字符串
int lastIndex=str.lastIndexOf("a");//返回字母“a”最后一次出现的索引位置返回字母“a”的索引位置otherIndex
int fiveBeforeIndex=str.lastIndexOf("a",5);//满足0<=fiveBeforeIndex<=5条件,在满足条件的结果集中,返回最大的数字
int threeBeforeIndex=str.lastIndexOf("a",3);//满足0<=threeBeforeIndex<=3条件,在满足条件的结果集中,返回最大的数字
System.out.println("字符串\"a\"最后一次出现的位置是:"+lastIndex);//输出信息
System.out.println("从索引位置5开始往回搜索,字母\"a\"最后一次出现的位置:"+fiveBeforeIndex);//输出信息
System.out.println("从索引位置3开始往回搜索,字母\"a\"最后一次出现的位置:"+threeBeforeIndex);//输出信息
}
- 3.4判断字符串首尾内容
starts With()方法和endsWith()方法分别用于判断字符串是否以指定的内容开始或结束。这两个方法的返回值都是boolean类型
startsWith(String prefix)
str.startsWitn(prefix);
str:任意字符串
prefix:作为前缀的字符串
public static void main(String[] args)