String字符串的常见方法总结

1.String.length():返回字符串长度。

public class work1 {
	public static void main(String[] args) {
		String ret = "黄飞鸿";
		System.out.println(ret.length()); // 3
	}
}

2、String.charAt(int index):返回某索引处的字符

public class work1 {
	public static void main(String[] args) {
		String ret = "黄飞鸿";
		System.out.println(ret.charAt(2)); // 鸿
	}
}

3、String.isEmpty():判断字符串是否为空

public class work1 {
	public static void main(String[] args) {
		String ret = "黄飞鸿";
		System.out.println(ret.isEmpty()); // false
	}
}

4、String.toUpperCase():将字符转换成大写

public class work1 {
	public static void main(String[] args) {
		String ret = "abc";
		System.out.println(ret.toUpperCase()); // ABC
	}
}

5、String.toLowerCase():将字符转换成小写

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		System.out.println(ret.toLowerCase()); // abc
	}
}

6、String.equals(Object obj):比较字符串的内容是否相等

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.equals(ret1)); // false
	}
}

7、String.equalsIgnoreCase(String anotherString):忽略大小写比较字符串内容是否相等

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.equalsIgnoreCase(ret1)); // true
	}
}

8、String.trim():去除首尾空白字符

public class work1 {
	public static void main(String[] args) {
		String ret = " ABC";
		String ret1 = "abc";
		System.out.println(ret.trim()); // ABC
	}
}

9、String.compareTo(String anotherString):比较两个字符串的大小

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.compareTo(ret1)); // -32
	}
}

10、String.substring(int beginIndex):返回一个新的字符串,它是原字符串从beginIndex开始截取到原字符串结尾的一个子字符串

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.substring(1)); // BC
	}
}

11、String.substring(int beginIndex,int endIndex):返回一个新的字符串,它是原字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.substring(0,2)); // AB
	}
}

12、String.endsWith(String suffix):测试此字符串是否以指定后缀结尾

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.endsWith("C")); // true
	}
}

13、String.startsWith(String prefix):测试此字符串是否以指定前缀开始

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.startsWith("B")); // false
	}
}

14、String.startsWith(String prefix,int toffset):测试此字符串是否从指定索引开始的子字符串是否以制定前缀开始

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.startsWith("B",1)); // true
	}
}

15、String.replace(char oldChar, char newChar):返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的。

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.replace("A", "D")); // DBC
	}
}

16、String.indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.indexOf("B")); // 1
	}
}

17、String.indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.indexOf("C",1)); // 2
	}
}

18、String.lastindexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.lastIndexOf("A")); // 2
	}
}

19、String.lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.lastIndexOf("A",1)); // 0
	}
}

20、String.concat():字符串的拼接

public class work1 {
	public static void main(String[] args) {
		String ret = "ABC";
		String ret1 = "abc";
		System.out.println(ret.concat(ret1)); // ABCabc
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值