/*
String 类中常用的方法
1. indexOf();根据字符返回字符串的索引(即字符串中对应字符的下标)
2. charAt(); 根据索引返回字符(通过字符串的下标找出对应的字符)
3. replace("旧的字符串","新的字符串"); 将字符串中旧的字符串替换成新的字符串
4. trim(); 去除字符串两端的空白
5. toUpperCase();字符串转换大写;toLowerCase()转换为小写
6. split();字符串分割,返回的是数组
7.substring(int beginIndex);这个的作用为截取从beginindex位置处的元素开始,默认截取至剩余所有。
substring(int beginIndex, int endIndex);这个的作用为截取从beginIndex开始,截取至endIndex-1位置间的元素
*/
import org.w3c.dom.ls.LSOutput;
public class Demo2 {
public static void main(String[] args) {
String str = " adergv btyytr ";
//1.indexOf()
System.out.println(str.indexOf('y'));
//2.charAt();
System.out.println(str.charAt(3));
//3.replace()
System.out.println(str.replace("ad","da"));
//4.trim()
System.out.println(str);
System.out.println(str.trim());
//5.toUpperCase(),toLowerCase()
System.out.println(str.toUpperCase());
str.toUpperCase();
System.out.println(str.toLowerCase());
//6.split
String[] arr = str.split(" ");//根据空格分割
for ( String s:
arr) {
System.out.println(s);
}
System.out.println(arr.length+","+arr[0]);
//7.substring()
System.out.println(str);
System.out.println(str.substring(2));//substring(int beginIndex)
System.out.println(str.substring(3,5));//substring(int beginIndex, int endIndex)
}
}
运行结果