equals 字符串比较值
当两个字符串要比较是否相等时
不能用双等号来比较
boolean b = msg.equals("abc");
System.out.println(b);
getBytes 获取字节数组
代码
byte[] bytes = msg.getBytes();
System.out.println(bytes);
for (byte b:bytes
) {
System.out.println(b);
}
结果
[B@4554617c
97
98
99
结论
在utf8的编码下
一个中文占三个字节
一个英文或符号占一个字节
toUpperCase toLowerCase 转换大小写
String s = msg.toUpperCase();
System.out.println(s);
String s = msg.toLowerCase();
System.out.println(s);
charAt 根据下标获取字符
char c = msg.charAt(2);
System.out.println(c);
compareTo 字符串比大小
单个字符串比较 ascii码值做减法
int a = "a".compareTo("c");
System.out.println(a);
多个字符的串做比较 差异性内容做ascii差值
int a = "abc9".compareTo("aba8");
System.out.println(a);
比较原则,如果前面的相同就从不同的那一个开始比起
多字符的串做比较 相同区比完后 剩余长度差做差值
两个字符串具有部分完全相同的内容
就会比较剩余的长度
用长度差作为返回结果
int a = "ab".compareTo("ab78912");
System.out.println(a);
concat 字符串的拼接
String concat = "abc".concat("123");
System.out.println(concat);
contains 判断是否包含
boolean b = "abc".contains("你");
System.out.println(b);
endsWith 判断结尾
boolean txt = "abc.txt".endsWith("txt");
System.out.println(txt);
replace 查找替换
String replace = "我正在学习java,java感觉不太难".replace("java", "js");
System.out.println(replace);
split 字符串的分割
String[] split = "刘备,关羽,张飞".split(",");
for (String s : split) {
System.out.println("欢迎新同学:" + s);
}
substring 字符串截取
一参,从指定的下标截取到未端
String substring = "0123456789".substring(3);
System.out.println(substring);
二参,设置起点与终点下标
String substring = "0123456789".substring(2, 5);
System.out.println(substring);
format 格式化字符串
String name = "pyhui";
int age = 23;
String fmt = "大家好我叫%s,今年%s岁";
String msg = String.format(fmt, name, age);
System.out.println(msg);
注意
format方法是静态方法
需要通过类名称来调用