package com.inmind.string01; /* 4.常用类-String-的获取方法_length_concat_charAt_indexOf_subString public int length () :返回此字符串的长度。 public String concat (String str) :将指定的字符串连接到该字符串的末尾。 public char charAt (int index) :返回指定索引处的 char值。 public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。 public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。 public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。 */ public class Demo04 { public static void main(String[] args) { String s = "hello world"; //我想截取world这个单词 //public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。 String substring = s.substring(6); System.out.println(substring); //我想截取hello这个单词 //public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。 System.out.println(s.substring(0, 5)); } public static void indexMethod(String[] args) { //public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。 String s = "123java321java123"; //查询下当前字符串是否含有java内容 int index = s.indexOf("java"); System.out.println(index);//3 //查询下当前字符串中第二个java内容的位置 int index1 = s.indexOf("java", 3 + 1); System.out.println(index1);//10 System.out.println(s.indexOf("xxx"));//-1 } public static void charAtMethod(String[] args) { //public char charAt (int index) :返回指定索引处的 char值。 String str = "hello world"; //获取空格字符 System.out.println(str.charAt(5)); System.out.println(str.charAt(6)); } public static void concatMethod(String[] args) { //public String concat (String str) :将指定的字符串连接到该字符串的末尾。 String s1 = "hello"; String s2 = "java"; //String s = s1+s2; String s = s1.concat(s2); System.out.println(s); } public static void lengthMethod(String[] args) { //public int length () :返回此字符串的长度。 String s = "123456"; System.out.println(s.length());//6 } }