字符串的获取相关方法以及字符串的截取
字符串的获取相关方法
String当中与获取相关的常用方法:
int lenth() 获取字符串当中含有的字符个数,拿到字符串长度
String concat(String str) 将当前字符串和参数字符串拼接成为返回值新的字符串
char charAt(int index) 获取指定索引位置的单个字符(索引从0开始)
int indexof(String str) 查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1。
public static void main(String[] args) { int length = "hakjgawhffa".length(); System.out.println("长度:"+length); String s1 = "Hello"; String s2 = "World"; String s3 = s1+s2; System.out.println(s1); System.out.println(s2); System.out.println("concat:"+s3); System.out.println("charAt:"+s1.charAt(1)); System.out.println("indexof:"+s1.indexOf("l")); }
运行结果:

字符串的截取方法
截取:
String substring(int index) 截取从参数位置一直到字符串末尾,返回新字符串。
String substring(int begin,int end) 截取从begin开始,一直到end结束,中间的字符串(只包含开头不包含结尾)
public static void main(String[] args) { String s = "HelloWorld"; String substring = s.substring(5); System.out.println(substring); String substring1 = s.substring(4, 7); System.out.println(substring1); }
运行结果:


浙公网安备 33010602011771号