String关键字

1. String方法

1.1 String类下面的方法
1.1.1 equals(object obj)

返回值为Boolean类型

语法格式

判断字符串是否相等
String str = "abc";
String str1 = "abc";
str.equals(str1);
返回值为true或false
1.1.2 lenght()

返回值为int类型

语法格式

查看字符串长度
str.lenght();
返回值为int类型
1.1.3 charAt(int index)

返回值为char类型

语法格式

通过索引下标 获取当前下标的字符
str.charAt(0);
返回值为char类型
1.1.4 indexOf(String str)

返回值为int类型

语法格式

获取指定字符第一个出现的下标
str.indexOf("a");
返回值为int类型
1.1.5 lastindexOf(int ch)

返回值为int类型

语法格式

获取指定字符最后一次出现的下标
str.lastindexOf('a');
返回值为int类型
1.1.6 endwith(String str)

返回值为Boolean类型

语法格式

判断该字符串是否以指定字符或字符串结尾的
str.endwith("a");
返回值为true或false
1.1.7 isEmpty()

返回值为Boolean类型

语法格式

判断字符串是否为空
str.isEmpty();
返回值为true或false
1.1.8 contains(String str)

返回值为Boolean

语法格式

判断字符串是否包含另一个字符串
str.contains("aaa");
返回值为true或false
1.1.9 equalslgnorecase();

返回值为Boolean

语法格式

忽略大小写比较两个字符串是否想等
str.equalslgnorecase();
返回值为true或false
1.2 String类下的构造方法【开发中要用】
1.2.1 String(char[] ch)【重点】

语法格式

将字符数组转为字符串
char[] ch = {'a', 'b', 'c'};
String str = new String(ch);
sout(str);
//abc
1.2.2 char[] tocharArry()方法【重点】

语法格式

将字符串转为字符数组
String str = "abc";
char[] ch = str.tocharArray();
1.3 String类下开发常用的方法【重点】
1.3.1 replace(char oldChar , char newChar)

语法格式

新的字符替换老的字符
str.replace('a','*');
1.3.2 split(int beginindex)

语法格式

以指定的字符串进行切割
切谁不要谁
str,split("b");
1.3.3 subString(int beginindex)

语法格式

从指定下标位置开始截取字符串
str.subString(2);
1.3.4 subString(int beginIndex, int endIndex)

语法格式

从指定位置开始到指定位置结束(要头不要尾)
str.subString(2,4);
1.3.5 toUpperCase()

语法格式

把字符串转为大写
str.toUpperCase();
1.3.6 toLowerCase()

语法格式

把字符串转为小写
str.toLowerCase();

1.3.7 trim()

语法格式

去掉首尾空格
str.trim();
 
posted @ 2022-07-29 20:17  早睡晚起身体好  阅读(54)  评论(0)    收藏  举报