String类的常用方法

public char charAt(int index)

【根据索引取得指定位置上的字符】

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		System.out.println(str.charAt(0));
	}
}

输出H。【字符串的索引从0开始计算】

返回值为char类型

public boolean endsWith(String suffix)

【判断字符串是否以指定的内容结束,如果是返回true,否则返回false】

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		System.out.println(str.endsWith("d"));
	}
}
public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		System.out.println(str.startsWith("H"));
	}
}

该方法区分大小写。

返回值为boolean类型

public byte【】 getBytes()

【将一个字符串转换成一个byte类型的数组返回】

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		byte [] bts = str.getBytes();
		System.out.println("字节数组的长度:"+bts.length);
	}
}

长度是11,空格也算

public int indexOf(String str)

【查询出指定内容在字符串中第一次出现的位置,如果没有指定的值返回-1】

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		System.out.println(str.indexOf("l"));
	}
}

三个l,只返回第一个l,返回2

public String intern()

【让字符串入池,转移到String常量池】

public class Hello {
	public static void main(String[] args) {
		String str1 ="Hello World";
		String str2=new String("Hello World");
		String str3= str2.intern();
		System.out.println(str1==str2);
	}
}

public boolean isEmpty()

【判断字符串的长度,如果长度为0返回true,否则返回false】

public class Hello {
	public static void main(String[] args) {
		String str1 ="";
		System.out.println(str1.length());
		System.out.println(str1.isEmpty());
	}
}

public int lastIndexOf(String str)

【返回指定内容在字符串中最后一次出现的位置,如果没有出现返回-1】

public class Hello {
	public static void main(String[] args) {
		String str1 ="Hello World";
		System.out.println(str1.lastIndexOf("o"));
	}
}

输出为7。

public boolean matches(String regex)

【使用指定正则匹配字符串,如果匹配上则返回true,否则返回false】

public class Hello {
	public static void main(String[] args) {
		String str1 ="Hello World";
		System.out.println(str1.matches("Hello World"));
	}
}

参数还可以是正则表达式,正则的内容后面花两节课时间讲解。

public String【】 split(String regex)

【把字符串按照指定的字符串拆分,拆分之后保存到一个字符串数组返回】

以空格拆分

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		String [] strs =str.split(" ");
		for(String temp:strs) {
			System.out.println(temp+" ");
		}
	}
}

以o拆分

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		String [] strs =str.split("o");
		for(String temp:strs) {
			System.out.println(temp+" ");
		}
	}
}
Hell 
 W 
rld

public String substring(int beginIndex)

【截取字符串,从指定的索引截取到最后,参数:beginIndex开始的索引下标】

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		System.out.println(str.substring(2));
	}
}
llo World

该方法切割的时候包括了开始的索引下标。

public String substring(int beginLndex,int endlndex)

【截取字符串,从索引截取到结束的索引】

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		System.out.println(str.substring(1,3));
	}
}
el

包括开始的索引不包括结束的索引

public String toLowerCase()

【将所有的字母换成小写】

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		System.out.println(str.toLowerCase());
	}
}

该方法不影响原字符串的内容。

public String toUpperCase()

【将所有的字母转换成大写】

public class Hello {
	public static void main(String[] args) {
		String str ="Hello World";
		System.out.println(str.toUpperCase());
	}
}

对原字符串没有影响。

public String trim()

【删除字符两端的空格,返回删除空格后的字符串】

public class Hello {
	public static void main(String[] args) {
		String str ="  Hello World  ";
		System.out.println(str.length());
		System.out.println(str.trim().length());
		System.out.println(str.length());
	}
}

总结:

1.掌握String类常用的方法

2.如果我们要分析一个方法(根据api文档或者别人写的方法),要从方法的返回值和方法的参数入手。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值