String类的常用方法

String的常用方法

charAt():获取指定下标位置的字符(返回指定索引处的字符)
indexOf():返回目标字符在字符串中的位置下标(返回指定字符第一次出现位置的索引)
lastIndexOf(): 返回指定字符最后一次出现位置的索引
length():返回字符串的长度
split(): 字符串分割
trim():去除前后空格
valueOf(): 将其他类型转换为字符串类型
substring():截取字符串
toLowerCase():字符串转小写
toUpperCase():字符串转大写
contains():是否包含目标字符串
startsWith():是否以目标字符串开头
endsWith():是否已目标字符串结束
isEmpty():字符串的长度是否为0length()==0结果为true
replace(): 字符串替换
equals():字符串比较
getBytes():返回字符串的byte类型数组

使用方法

charAt()
String s = "abc";
char s2= s.charAt(1);
System.out.println(s2);    //b
indexOf()
String s = "abccc";
int s2 = s.indexOf("c");
System.out.println(s2);    //2
lastIndexOf()
String s = "abccc";
int s2 = s.lastIndexOf("c");
System.out.println(s2);    //4
length()
String s = "abc";
int s2 = s.length();
System.out.println(s2);    //3
split()
String s = "123&456";
String[] values = s.split("&");
String s1 = values[0];
String s2 = values[1];
System.out.println(s1);   //123
System.out.println(s2);   //456
trim()
String s = "  ab c   ";
String s2 = s.trim();
System.out.println(s2);    //ab c
valueOf()

int类型转String类型

int i = 5;
String s = String.valueOf(s);
System.out.println(s);    // "5"

String类型转int类型

String s = "123";
int i = Integer.parseInt(s);
System.out.println(i);    // 123
substring()
用法1:
String s = "abcdefg";
String s2 = s.substring(3);
System.out.println(s2);   //defg
用法2:
String s = "abcdefg";
String s2 = s.substring(2,4);  //[2,4)
System.out.println(s2);   //cd
toLowerCase()
String s = "ABCabc";
String s2 = s.toLowerCase();
System.out.println(s2);   //abcabc
toUpperCase()
String s = "ABCabc";
String s2 = s.toUpperCase();
System.out.println(s2);   //ABCABC
contains()
//注意:此方法区分大小写。
String s1 = "ABCDE";
String s2 = "CD";
System.out.println(s1.contains(s2));  //true
startsWith()
String s = "abcd";
System.out.println(s.startsWith("ab"));  //true
endsWith()
String s = "abcd";
System.out.println(s.endsWith("d"));  //true
isEmpty()
String s = "";
boolean s2 = s.isEmpty();
System.out.println(s2);   //true
replace()
String s = "abcd";
String s2 = s.replace("b","F");
System.out.println(s2);   //aFcd
equals()
String s1 = "abc";
String s2 = "abc";
System.out.println(s1.equals(s2));  //true

组合使用

获取图片名称后缀
String s = "123.jpg";
int s1 = s.lastIndexOf(".");
String s2 = s.substring(s1);
System.out.println(s2);   // .jpg

java8-Api

学习文档:https://www.matools.com/api/java8



       卑微的讨好别人,只会换来别人的无视,这世界只有自身的强大,才能换来别人对你的重视,只有平等的对待,才能换来真正的尊重。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值