*String类
- CharSequence
-
String, StringBuffer, StringBuilder
- Public Void Show(CharSequence, seqence);
- char charAt(int index)
-
返回指定索引的char值
- int length()
-
返回此字符序列的长度
- CharSequence subSequence(int start, int end)
-
返回一个新的Charsequence,它是此序列的字序列
- String toString()
-
返回一个包含此序列中字符的字符串,该字符串与此序列的顺序想同
*String 特点
-
1.String 类代表字符串,Java程序中的所有字符串字面值(如abc)都作为此类的实例实现
-
2.字符串是常量,它们的值在创建之后不能更改
-
3.所有的常量在内存的方法区的常量池中
-
4.频繁的字符串拼接操作会导致常量池容器增加,浪费内存资源
-
5.字符串缓存区[StringBuffer, StringBuilder]支持可变的字符串
-
6.Java在编译时期会针对字符串的拼接操作作用StringBuilder改进,,提高效率,节约内存资源
-
s = (new StringBuilder((String.valueof(s)).apped("efg").toString();
-
System.out.println(new StringBuilder("s:").append(s).toString();
-
7.因为String 对象是不可变的,所以可以共享
-
8.字符串本质是字符数组
- 常量:
-
字面值常量:"abc"是一个对象
-
自定义常量:String s = "abc";
// 利用多态来创建字符串 char charAt(int index)
CharSequence sequence = "abc";
String s = "HelloWorldHello";
System.out.println("charAt " + s.charAt(0));
System.out.println("charAt " + s.charAt(1));
// 遍历字符串没一个字符 int indexof(int ch)
for (int i = 0; i < s.length(); i++) {
System.out.println("charAt " + s.charAt(i));
}
// int indeof(int ch)
System.out.println("indexof(int ch): " + s.indexOf("l"));
// int indexof(String str)返回某个字符串对应的索引
System.out.println("int indexof(String str): " + s.indexOf("World"));
// 从fromindex处开始从左往右找,第一次出现字符ch所对应的索引
System.out.println("int indexof(int ch, int fromindex): " + s.indexOf('l', 4));
// 从字符串左往右开始找,第一次出现字符串所对应的位置
System.out.println("int indexof(String string, int fromindex): " + s.indexOf("Hello", 6));
// 从右往左开始找第一次字符对应的索引
System.out.println("int lastIndexof(int ch): " + s.lastIndexOf('l'));
// 从右往左找,第一次出现的
System.out.println("int lastIndexof(int ch, int fromIndex): " + s.lastIndexOf('l', 10));
// 从右往左找,第一个字符串对应的索引
System.out.println("String string, int fromIndex: " + s.lastIndexOf("Hello", 10));
// 截取
s.substring(10);
System.out.println(s);
String s1 = s.substring(5, 10); //左闭右开原则
System.out.println(s1);
CharSequence subSequence = s.subSequence(5, 10);
System.out.println(sequence);
String s = "HelloWorld";
// 将字符串转化为字节数组
byte[] bys = s.getBytes();
System.out.println(Arrays.toString(bys));
for (byte b : bys) {
System.out.println((char)b);
}
System.out.println("------------");
// 将字符串转化为字符数组
char[] ch = s.toCharArray();
for (char c : ch) {
System.out.println(c);
}
System.out.println("-------------");
// 返回char数组参数的字符串表示
String ss1 = String.valueOf(false);
System.out.println(ss1);
String ss3 = String.valueOf(2.5);
String ss2 = String.valueOf(10);
System.out.println(ss2 + ss3);
String ss4 = String.valueOf(new Object());
System.out.println(ss4);
System.out.println("--------------");
String toUpperCase = "select * from t_student".toUpperCase();
System.out.println(toUpperCase);
String lowerCase = toUpperCase.toLowerCase();
System.out.println(lowerCase);
System.out.println("---------------");
System.out.println("Hello".concat("World"));
*String replace(char old.char new): 替换功能
*String replace(String old, String new):替换功能
*String trim(): 去除字符串的空格
*int compareTo(Sting str): 按字典顺序比较两个字符串
*int compareToIgnoreCase(String str): 按字典顺序比较两个字符串,忽略
*public String[] split(String regex): 分隔字符串成字符数组
*/
public class SeatWork4 {
public static void main(String[] args) {
String s = "HelloWorld世界你好HellowWorld";
System.out.println(s.replace('e', 'c'));
String s2 = " Hello ";
System.out.println(s2.trim() + "-=====");
String s3 = "abcd";
String s4 = "abce";
System.out.println(s3.compareTo(s4));
// 中文比较 后面使用比较器compare的核心
Collator collator = Collator.getInstance(Locale.CHINA);
int compare = collator.compare("贼", "蠢");
System.out.println(compare);
// 分隔
String s5 = "hellow_world";
String s6 = "hellow123456world55551";
String[] strs = s5.split("_");
System.out.println(Arrays.toString(strs));
System.out.println(Arrays.toString(s6.split("\\d")));
// 合并
CharSequence[] chs = {"hellow", "world"};
String join = String.join("+", chs);
System.out.println(join);
}
}