String是在开发中常见的数据类型,也是使用做多的的数据类型。
-
根据索引取得指定位置上的字符
public char charAt(int index)
字符串索引是从0开始计算 -
判断字符串是否以指定内容结束或者开始,如果是返回true,否则返回false。
public boolean endsWith(String suffix)
public boolean startsWith(String suffix) -
将一个字符串转化成一个byte类型的数组返回
public byte[] getBytes() -
查询出指定内容在字符串中第一次出现的位置
public int indexOf(String str) -
让字符串入池,转移到String常量池
public String intren() -
判断字符串的长度,如果长度为0,返回true,否则false
public boolean isEmpty() -
返回指定内容在字符串中最后一次出现的位置,如果没有出现返回-1
public int lastindexOf(String str) -
使用指定正则匹配字符串,如果匹配上着返回true,否则false
public boolean matches(String regex) -
把字符串按照指定的字符串拆分,拆分之后保存到一个字符串数组返回
public String[] split(String regex) -
截取字符串,从指定的索引截取到最后
public String substring(int beginIndex)
beginIndex:开始的索引下标 -
截取字符串,从开始索引截取到结束索引之前
public String substring(int beginIndex,int endIndex) -
将字符串的所有字母转换成小写
public String toLowerCase()
不影响原字符串的内容 -
将字符串的所有字母转换成大写
public String toUpperCase()
不影响原字符串的内容 -
删除字符串两端的空格,返回删除空格后的字符串
public String trim()
public class Zfc{
public static void main(String[] args) {
String s="Hello World";
String s1= new String(s);
String s2=s1.intern();//入池
System.out.println(s==s2);//true
byte [] str =s.getBytes();//转成数组
System.out.println(s.trim())
System.out.println(s.toUpperCase());
System.out.println(s.substring(1,3));
String [] str2=s.split("o");
System.out.println(s.lastIndexOf("o"));
System.out.println(s.matches("hello World"));
System.out.println(s.empty());
System.out.println(s.length());
System.out.println(s.indexOf("o"));
System.out.println(s.charAt(0));
System.out.println(s.endsWith("d"));//是否以结尾区分大小写
System.out.println(s.startsWith("h"));
}
}