String常用API

String str="abcdefghijkl";

1、char chr=str.charAt(0);//返回指定索引处的字符
2、int i = str.compareTo(“123456”);//比较连个字符串的 ASCII码
3、int res = str.compareToIgnoreCase(“ABC”);// 按字典顺序比较两个字符串,不考虑大小写。
4、String concat = str.concat(“123”);//在字符串后面拼接另一个字符串
5、boolean res = str.endsWith(“com”);//判断字符串以什么字符串结尾
6、boolean equals = str.equals(“123”);//比较两个字符串是否一样
7、byte[] bytes = str.getBytes();//字符串转数组
byte[] bytes1 = str.getBytes(“utf-8”);//字符串转指定编码数组
8、String str=“www.baidu.com”;
char[] str2=new char[5];
str.getChars(0,5,str2,0);//将一个字符串指定范围存到,字符数组中
9、 int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。
int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
10、 int lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。
11、 int length()
返回此字符串的长度。
12、 boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。
13、 String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
14、 String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
15、 boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
16、 boolean startsWith(String prefix, int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
17、 String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
18、 char[] toCharArray()
将此字符串转换为一个新的字符数组。
19、 String trim()
返回字符串的副本,忽略前导空白和尾部空白。
20、 contains(CharSequence chars)
判断是否包含指定的字符系列。
21、 isEmpty()
判断字符串是否为空。

StringUtils工具类

必须依赖lang3包

org.apache.commons
commons-lang3
3.3.2

lang3是lang包的升级版

String str = “www.baidu.com”;
boolean b = StringUtils.startsWithAny(str, “query”, “find”);//判断字符串是否以什么开头
boolean b = StringUtils.endsWithAny(“1”, “2”, “3”);//判断字符串是否以什么结尾
StringUtils工具类isEmpty()和isBlank()的区别?
当需要判断指定字符串是否为空"“与null时使用isEmpty()
当有特殊要求,例如空格也算为空,” “,\t\r\n这种默认视为空的可以使用isBlank()
在判断”"与null时二者可以互相替换,若有空格isEmpty()是false的但是isBlank()就是true

String s = StringUtils.substringBetween(“a”, “b”, “c”);
在第一个字符串中取,后两个字符串中间的字符串

String repeat = StringUtils.repeat("", 10);
得到将
重复10此的字符串

String repeat = StringUtils.center(“china”, 11,"");//repeat = china
将第一个字符插入到多个重复的
中间

String strip = StringUtils.strip(“abca”, “a”);//bc
去掉字符串两端字符串

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值