简单使用:
public class APITest {
public static void main(String[] args){
String name="Machin.echjidhu ";
System.out.println("字符串的长度为:"+name.length());
System.out.println("字符串是否为空:"+name.isEmpty());
System.out.println("下标为5的字符是:"+name.charAt(5));
System.out.println("字符串是否为 \"Machine\":"+name.equals("Machine"));
System.out.println("c字符第一次出现的下标是:"+name.indexOf('c'));
System.out.println("c字符第一次出现的下标是:"+name.lastIndexOf('c'));
System.out.println("字符串是否以\"Mac\"开头"+name.startsWith("Mac"));
System.out.println("字符串是否为hu结束"+name.endsWith("hu"));
System.out.println("字符是否包含:"+name.contains("ech"));
System.out.println("将字符串变成大写:"+name.toUpperCase());
System.out.println("将字符串变成小写:"+name.toLowerCase());
System.out.println("返回一个字符串:"+String.valueOf(89));
char[] oe = name.toCharArray();
System.out.print("字符串转化为字符数组后为:");
for(int i=0;i<name.length();i++){
if(i!=name.length()-1){
System.out.print(oe[i]+",");
}
else{
System.out.print(oe[i]+"\n");
}
}
System.out.println("字符串中的ine用eni代替:"+name.replace("ine", "eni"));
String[] a=name.split("\\.");
System.out.println("字符串分割成.左右的字符串:"+a[0]+","+a[1]);
System.out.println("返回下标值4及后面的字符串"+name.substring(4));
System.out.println("返回下标值4后面(包括4)9前面(不包括9)的字符串"+name.substring(4,9));
System.out.println("去除首尾空格的字符串:"+name.trim());
}
}

本文提供了一个简单的Java程序示例,展示了如何使用字符串的各种方法进行基本操作,如获取长度、检查内容、查找特定字符的位置等。这些操作对于日常编程非常有用。
之String类的常见操作&spm=1001.2101.3001.5002&articleId=77479994&d=1&t=3&u=cc55b16fe0bb40f99c87c424a220ed84)
1904

被折叠的 条评论
为什么被折叠?



