Java String包装类常用方法

Java String包装类常用方法

String中方法的介绍

public char charAt(int index)

返回指定索引处的 char 值

public int compareTo(String anotherString)

按字典顺序比较两个字符串

返回值的解释
返回值等于零:两个字符串相等
返回值小于零:字符字典顺序比参数字符串前
返回值大于零:字符字典顺序比参数字符串后

public int compareToIgnoreCase(String str)

按字典顺序比较两个字符串,不考虑大小写

public String concat(String str)

类似于加号,将指定字符串连接到此字符串的结尾

public boolean contains(CharSequence s)

当此字符串包含指定的 char 值序列时,返回 true

public static void main(String[] args) {
		String str = "abcefg";		
		System.out.println(str.contains("fg"));//返回true
	}

public boolean startsWith(String prefix)

测试此字符串是否以指定的前缀开始。

public static void main(String[] args) {
		String str = "abcefg";		
		System.out.println(str.startsWith("abc"));//返回true
}

public boolean endsWith(String suffix)

测试此字符串是否以指定的后缀结束。

public boolean equals(Object anObject)

将此字符串与指定的对象比较。
需要注意 “==” 比较的是地址,也就是是否为同一个对象

public boolean equalsIgnoreCase(String anotherString)

将此 String 与另一个 String 比较,不考虑大小写。

public static String format(String format, Object… args)

格式化字符串

public int indexOf(String str)

返回指定子字符串在此字符串中第一次出现处的索引,如果没有找到,返回-1

public int lastIndexOf(String str)

返回指定子字符串在此字符串中最右边出现处的索引

public int length()

返回此字符串的长度

public String replace(char oldChar, char newChar)

返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的

public String replace(CharSequence target, CharSequence replacement)

使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串

public String[] split(String regex)

根据给定正则表达式的匹配拆分此字符串

public String substring(int beginIndex)

返回一个新字符串,它是此字符串的一个子字符串。

public char[] toCharArray()

将此字符串转换为一个新的字符数组

public String toLowerCase()

转换为小写

public String toUpperCase()

转换为大写

public String trim()

返回字符串的副本,忽略前导空白和尾部空白

public static String valueOf()

把其他类型转换成字符串类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值