Java中String 常用的方法

Java中String 常用的方法

一、String字符串

String是不可变对象,所有对于String的修改操作,其实都是使用一个新的对象来代替原来的对象

String str = "abc";
		str += "b";
		System.out.println(str);

GC会回收堆中所有符合垃圾回收的对象,符合垃圾回收的对象是堆中的对象没有被任何引用所指向

System.gc();//提醒GC回收垃圾

如果某一个字符串会被频繁修改,不建议使用String类型

二、String 常用的方法
  1. 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());
  1. char charAt(int index) 返回字符串中下标为index的字符
System.out.println(s.charAt(1));
  1. byte[] getBytes(String charsetName) 将字符串转换成字节数组,按照charsetName的编码格式,一般使用在IO
try {
			System.out.println(Arrays.toString(s.getBytes("utf-8")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
  1. boolean equals(Object anObject) 将字符串和anObject对象进行比较,是否相同
System.out.println(s.equals("小王八是一个痴情的人"));
		System.out.println(s.equals(123));
  1. boolean equalsIgnoreCase(String anotherString) 比较字符串和anotherString这个字符是否相同,不区分大小写
System.out.println("abc".equalsIgnoreCase("Abc"));
  1. int compareTo(String anotherString) 是用来做字符串的排序方法 返回整数比anotherString大
System.out.println("ac".compareTo("ab"));
  1. boolean startWith(String prefix) 判断字符串是否以prefix开头
System.out.println(s.startsWith("小王八"));
  1. boolean endtWith(String suffix) 判断字符串是否以suffix结尾
System.out.println(s.endsWith("人"));
  1. int hashCode() 返回字符串的hashCode值,主要用于和equals方法来辅助HashSet和HashMap查找和插入
System.out.println(s.hashCode());
  1. int indexOf(int ch) 返回字符串中unicode编码是ch的符号所处的位置,如果没找到就返回-1
System.out.println("abc".indexOf(99));
  1. int indexOf(String str) 返回字符串中str子字符串第一次出现位置
System.out.println("hello hello".indexOf("he"));
  1. int indexOf(Strinf str,int int fromIndex)返回字符串中str子字符串从下标为fromIndex位置开始第一次出现的下标
System.out.println("hello hello".indexOf("he",7));
  1. int lastIndexOf(String str) 返回字符串中str子字符串最后一次出现的位置
System.out.println("hello hello".lastIndexOf("he"));
  1. String substring(int beginIndex) 返回字符串从下标为beginIndex开始的子字符串
System.out.println(s.substring(2));
  1. String substring(int beginIndex,int endIndex)
    返回字符串从下标为beginIndex开始到endIndex结束的位置的子字符串,包含下标beginIndex,不包含endIndex
System.out.println(s.substring(1,5));
  1. String concat(String str) 返回字符串拼接上str字符串之后的结果,原来的字符串没有改变
System.out.println(s.concat(",但是凤凤去了美国"));
		System.out.println(s);
  1. String replace(char oldChar, char newChar) 返回将字符串中oldChar字符替换成newChar这个字符之后的新字符串,原字符串依然不变
System.out.println("abca".replace('a', 'd'));
  1. 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}$"));
  1. String replaceAll(String regex, String replacement) 将字符串中符合正则表达式regex的部分全部替换成replacement字符串
System.out.println("xxx你xxxx样".replaceAll("x", "*"));
  1. //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());
  1. String trim() 去掉字符串左右两边的空格,然后返回新的字符串,原字符串不变
		String sss = " 1 2 3 4 ";
		System.out.println(sss.trim());
		System.out.println(sss);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值