String类的常用方法

String是在开发中常见的数据类型,也是使用做多的的数据类型。

  1. 根据索引取得指定位置上的字符
    public char charAt(int index)
    字符串索引是从0开始计算

  2. 判断字符串是否以指定内容结束或者开始,如果是返回true,否则返回false。
    public boolean endsWith(String suffix)
    public boolean startsWith(String suffix)

  3. 将一个字符串转化成一个byte类型的数组返回
    public byte[] getBytes()

  4. 查询出指定内容在字符串中第一次出现的位置
    public int indexOf(String str)

  5. 让字符串入池,转移到String常量池
    public String intren()

  6. 判断字符串的长度,如果长度为0,返回true,否则false
    public boolean isEmpty()

  7. 返回指定内容在字符串中最后一次出现的位置,如果没有出现返回-1
    public int lastindexOf(String str)

  8. 使用指定正则匹配字符串,如果匹配上着返回true,否则false
    public boolean matches(String regex)

  9. 把字符串按照指定的字符串拆分,拆分之后保存到一个字符串数组返回
    public String[] split(String regex)

  10. 截取字符串,从指定的索引截取到最后
    public String substring(int beginIndex)
    beginIndex:开始的索引下标

  11. 截取字符串,从开始索引截取到结束索引之前
    public String substring(int beginIndex,int endIndex)

  12. 将字符串的所有字母转换成小写
    public String toLowerCase()
    不影响原字符串的内容

  13. 将字符串的所有字母转换成大写
    public String toUpperCase()
    不影响原字符串的内容

  14. 删除字符串两端的空格,返回删除空格后的字符串
    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"));

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值