API 学习——String、StringBuffer、StringBulider(线程不安全,方法同StringBuffer类同不再赘述)
String是java中经常使用的类型哈,闲着无聊就想看看呗。
import java.util.Locale;
/**
* @Title: SpringAPITest
* @Description: Spring字符串常用API练习
* @author: wangsai
* @date 2019/9/1 16:15
*/
public class SprringAPITest {
public static void main(String[] args) {
String string = " good good study,day day up";
String string1 = new String("2");
String string2 = "WANG SAI";
String string3 ="du ying rui";
StringBuffer stringBuffer = new StringBuffer("王赛真好");
//返回字符串该下标的字符
System.out.println("返回字符串该下标的字符"+string.charAt(3));
//将str字符串分割为四个子字符串,一般化会和上面的几个混用
System.out.println("将str字符串分割为四个子字符串,一般化会和上面的几个混用: "+string.split(" "));
//???
System.out.println("不知道什么效应: "+string.split(" ",1));
//将String类型转换为int类型
int i = Integer.valueOf(string1);
System.out.println("将String类型转换为int类型: "+i);
//去除字符串开头和结尾的空字符(空格,tab等)
System.out.println("去除字符串开头和结尾的空字符(空格,tab等): "+string.trim());
//截取一串字符串的一串子字符串,从from位置的字母(包括from)到end(不包括end位置)的字符串
System.out.println("截取一串字符串的一串子字符串(空格也计入了): "+string.substring(3,12));
//booelan startsWith(str):判断字符串是否是以参数str指定的内容开始
System.out.println("判断字符串是否是以参数指定的内容开始"+string.startsWith("g"));
//boolean endsWith(str); 常用于判断文件后缀
System.out.println("常用于判断文件后缀:"+string.endsWith("uvc"));
//统一转换为大写或者是小写
System.out.println("统一转换为大写:"+string.toUpperCase(Locale.forLanguageTag(string)));
//统一转换为大写或者是小写
System.out.println("统一转换为小写:"+string2.toLowerCase());
System.out.println("统一转换为小写:"+string.toLowerCase(Locale.forLanguageTag(string2)));
//xx对象.toString();必须先创建对象,再调用对象的toString()方法
//String.valueOf(XX对象):静态方法,不需要创建任何对象,就可以直接调用
//int compareTo(String anotherString) //区分大小写
//int compareToIgnoreCase(String str) //不区分大小写
System.out.println("将此字符串与指定的对象比较:"+string2.compareTo(string3));
//将指定字符串连接到此字符串的结尾
System.out.println("将指定字符串连接到此字符串的结尾:"+string2.concat(string));
//indexOf返回指定字符(字符串)索引
/*int indexOf(char ch||String str) 返回指定字符(字符串)在此字符串中第一次出现处的索引
int indexOf(char ch||String str, int fromIndex) 返回在此字符串中第一次出现指定字符(字符串)处的索引,从指定的索引开始搜
int lastIndexOf(char ch||String str) 返回指定字符(字符串)在此字符串中最后一次出现处的索引
int lastIndexOf(char ch||String str,int fromIndex) 返回指定字符(字符串)在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索*/
//获取字符串长度length():
System.out.println("获取字符串长度length():"+string.length());
//boolean isEmpty() 如果 length() 为 0,则返回 true;否则返回 false
System.out.println("判空:"+string.isEmpty());
/*String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。 参数:beginIndex起始索引(包括)。
String substring(int beginIndex,int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。 参数:beginIndex起始索引(包括);endIndex结束索引(不包括)。*/
System.out.println("倒序输出:"+stringBuffer.reverse());
}
}
<详细学习请查阅JDK官方文档>
StringBuffer:
String API学习
String API学习
String API学习
String API学习
String spilt用法
String spilt用法
String split用法
有兴趣去的可以去补充一下StringBuffer的API相关知识等等