String属于引用类型,关于String的简单方法有:length()、charAt(index)、concat(s1)、toUpperCase()、toLowerCase()、trim(),由于这些方法只能从一个特定的字符串来调用,故也称为实例方法。
String s3=s1.concat(s2);//连接两个字符串
此语句等价于:
String s3=s1+s2;
加号若连接字符串和其他类型的值时,其他类型自动转化为字符串。
若i=1,j=2,则System.out.println("i+j is"+i+j)得到的结果不是i+j is 3,而是i+j is 12。
从控制台读取字符串用input.next(),而读取一整行文本用input.nextLine()。
String对象的比较方法:
equals(s1)、equalsIgnoreCase(s1)、compareTo(s1)、compareToIgnoreCase(s1)、startsWith(prefix)、endsWith(prefix)、contains(s1)。
操作符==只能检测str1和str2是否指向同一个对象,却不能比较内容是否相同。
String类包含的获取字串的方法:
substring(beginIndex)、substring(beginIndex,endIndex)、indexOf(ch)、indexOf(ch,fromIndex)(返回字符串中fromIndex之后出现的第一个ch的下标)、laseIndexOf(ch)、lastIndextOf(ch,fromIndex)、lastIndextOf(s)、lastindexOf(s,fromIndex)