Java中String 常用的方法
一、String字符串
String是不可变对象,所有对于String的修改操作,其实都是使用一个新的对象来代替原来的对象
String str = "abc";
str += "b";
System.out.println(str);
GC会回收堆中所有符合垃圾回收的对象,符合垃圾回收的对象是堆中的对象没有被任何引用所指向
System.gc();//提醒GC回收垃圾
如果某一个字符串会被频繁修改,不建议使用String类型
二、String 常用的方法
- int length();获取字符串对象的长度,仅表示字符的个数
String s = "小王八是个傻逼";
System.out.println(s.length());
注意:
length() 字符串长度
length 数组的长度属性
size() 集合的元素个数
2.boolean isEmpty() 判断字符串是否为空
null 对象为空
" " 空对象 等价于 str.length == 0
null.isEmpty(); //空指针异常
"".isEmpty(); //true
System.out.println("".isEmpty());
- char charAt(int index) 返回字符串中下标为index的字符
System.out.println(s.charAt(1));
- byte[] getBytes(String charsetName) 将字符串转换成字节数组,按照charsetName的编码格式,一般使用在IO
try {
System.out.println(Arrays.toString(s.getBytes("utf-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
- boolean equals(Object anObject) 将字符串和anObject对象进行比较,是否相同
System.out.println(s.equals("小王八是一个痴情的人"));
System.out.println(s.equals(123));
- boolean equalsIgnoreCase(String anotherString) 比较字符串和anotherString这个字符是否相同,不区分大小写
System.out.println("abc".equalsIgnoreCase("Abc"));
- int compareTo(String anotherString) 是用来做字符串的排序方法 返回整数比anotherString大
System.out.println("ac".compareTo("ab"));
- boolean startWith(String prefix) 判断字符串是否以prefix开头
System.out.println(s.startsWith("小王八"));
- boolean endtWith(String suffix) 判断字符串是否以suffix结尾
System.out.println(s.endsWith("人"));
- int hashCode() 返回字符串的hashCode值,主要用于和equals方法来辅助HashSet和HashMap查找和插入
System.out.println(s.hashCode());
- int indexOf(int ch) 返回字符串中unicode编码是ch的符号所处的位置,如果没找到就返回-1
System.out.println("abc".indexOf(99));
- int indexOf(String str) 返回字符串中str子字符串第一次出现位置
System.out.println("hello hello".indexOf("he"));
- int indexOf(Strinf str,int int fromIndex)返回字符串中str子字符串从下标为fromIndex位置开始第一次出现的下标
System.out.println("hello hello".indexOf("he",7));
- int lastIndexOf(String str) 返回字符串中str子字符串最后一次出现的位置
System.out.println("hello hello".lastIndexOf("he"));
- String substring(int beginIndex) 返回字符串从下标为beginIndex开始的子字符串
System.out.println(s.substring(2));
- String substring(int beginIndex,int endIndex)
返回字符串从下标为beginIndex开始到endIndex结束的位置的子字符串,包含下标beginIndex,不包含endIndex
System.out.println(s.substring(1,5));
- String concat(String str) 返回字符串拼接上str字符串之后的结果,原来的字符串没有改变
System.out.println(s.concat(",但是凤凤去了美国"));
System.out.println(s);
- String replace(char oldChar, char newChar) 返回将字符串中oldChar字符替换成newChar这个字符之后的新字符串,原字符串依然不变
System.out.println("abca".replace('a', 'd'));
- boolean matches(String regex) 判定字符串是否符合正则表达式regex的规定
System.out.println("1311234567890".matches("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|"
+ "(17[0-8])|(18[0-9])|(19[0-3,5-9]))[0-9]{8}$"));
- String replaceAll(String regex, String replacement) 将字符串中符合正则表达式regex的部分全部替换成replacement字符串
System.out.println("xxx你xxxx样".replaceAll("x", "*"));
- //String[] split(String regex) 将字符串按照正则表达式regex切割成字符串数组
String ss = "1a34dc37b8九";
String[] arr = ss.split("[a-z]+");//? 0个或1个 + 1个或1个以上 * 0个或0个以上 \d表示数字 \D非数字
System.out.println(Arrays.toString(arr));
System.out.println(ss);
22.String toLowerCase() 将字符串中的所有字母全部转换成小写字母
String toUpperCase() 将字符串中的所有字母全部转换成大写字母
System.out.println("aBc".toUpperCase());
System.out.println("aBc".toLowerCase());
- String trim() 去掉字符串左右两边的空格,然后返回新的字符串,原字符串不变
String sss = " 1 2 3 4 ";
System.out.println(sss.trim());
System.out.println(sss);