关于String的常用方法
关于String的常用方法:
1.charAt(int index):返回指定索引处的值:
public static void main(String[] args) {
String str = "abcf";
System.out.println(str.charAt(0));// a
}
2.concat(String str):将指定的字符串连接到该字符串的末尾
public static void main(String[] args) {
String str = "abcf";
System.out.println(str.concat("llll"));// abcfllll
}
3.equals(Object o):将此字符串与指定对象进行比较
public static void main(String[] args) {
String str1 = "abcf";
String str2 = "abcf";
System.out.println(str1.equals(str2));// true
}
4.length():返回字符串的长度
public static void main(String[] args) {
String str1 = "abcf";
System.out.println(str1.length());// 4
}
5.indexOf(String str):返回指定字符串第一次出现在字符串内的索引
public static void main(String[] args) {
String str1 = "abcf";
System.out.println(str1.indexOf("bc"));// 1
}
6.replace(char oldChar,char newChar):将指定字符串替换为新的字符串
public static void main(String[] args) {
String str1 = "abcf";
System.out.println(str1.replace("a","ccc"));// cccbcf
}
7.split():字符串分割
public static void main(String[] args) {
String str = "ssssefefdf";
String[] split = str.split("");
System.out.println(Arrays.toString(split));//[s, s, s, s, e, f, e, f, d, f]
}
8.subString(int beginIndex):字符串截取
public static void main(String[] args) {
String str = "ssssefefdf";
String substring = str.substring(1);
System.out.println(substring);//sssefefdf
}
9.toCharArray():将字符串转化为一个字符数组
public static void main(String[] args) {
String str1 = "abcf";
char[] a = str1.toCharArray();
System.out.println(a);// abcf
}
10.toUpperCase():将字符串转化为大写:
public static void main(String[] args) {
String str = "SSsefefdf";
System.out.println(str.toUpperCase());// SSSEFEFDF
}
11.toLowerCase():将字符串转化为小写:
public static void main(String[] args) {
String str = "SSsefefdf";
System.out.println(str.toLowerCase());// sssefefdf
}
12.valueOf(int a):int类型转字符串(这里可以是char,double,float等)
public static void main(String[] args) {
int a = 90;
String str = String.valueOf(a);
System.out.println(str);//90
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!