String类的获取功能:String类的基本获取功能、获取功能的举例子、String类的基本转换功能、转换功能的举例子、

1、String类的获取功能:
(1)int length()
获取字符串的长度,即字符串中字符的个数。
(2)char charAt(int index)
获取指定索引位置上的字符。
(3)int indexOf(int ch)
获取指定字符在此字符串中第一次出现的索引。注意:这里用的是int,不是char,原因是'a'和97都可以作为实参传入。
(4)int indexOf(String str)
获取指定字符串在此字符串中第一次出现的索引。
(5)int indexOf(int ch,int fromIndex)
获取指定字符在此字符串中指定位置后第一次出现的索引。
(6)int indexOf(String str,int fromIndex)
获取指定字符串在此字符串中指定位置后第一次出现的索引。
(7)String substring(int start)
从指定位置截取子字符串,默认是截取到末尾。(包含start位置)
(8)String substring(int start,int end)
从指定位置开始到指定位置结束截取子字符串。(包start不包end)
2、获取功能的举例
package cn.itcast_06;
public class StringDemo {
public static void main(String[] args) {
// int length()
// 获取字符串的长度,即字符串中字符的个数。
String s="helloworld";
System.out.println("length():"+s.length());//10
System.out.println("--------------");
// char charAt(int index)
// 获取指定索引位置上的字符。
System.out.println("charAt:"+s.charAt(0));//h
System.out.println("charAt:"+s.charAt(9));//d
System.out.println("--------------");
// int indexOf(int ch)
// 获取指定字符在此字符串中第一次出现的索引。注意:这里用的是int,不是char,
// 原因是'a'和97都可以作为实参传入。
System.out.println("indexOf:"+s.indexOf('h'));//0
System.out.println("indexOf:"+s.indexOf('d'));//9
System.out.println("--------------");
// int indexOf(String str)
// 获取指定字符串在此字符串中第一次出现的索引。
System.out.println("indexOf:"+s.indexOf("owo"));//4
System.out.println("indexOf:"+s.indexOf("ld"));//8
System.out.println("--------------");
// int indexOf(int ch,int fromIndex)
// 获取指定字符在此字符串中指定位置后第一次出现的索引。
// int indexOf(String str,int fromIndex)
// 获取指定字符串在此字符串中指定位置后第一次出现的索引。
System.out.println("indexOf:"+s.indexOf('l',4));//8
System.out.println("indexOf:"+s.indexOf('l',40));//-1
System.out.println("--------------");
// String substring(int start)
// 从指定位置截取子字符串,默认是截取到末尾。(包含start位置)
System.out.println("substring:"+s.substring(4));//oworld
System.out.println("substring:"+s.substring(0));//helloworld
// String substring(int start,int end)
// 从指定位置开始到指定位置结束截取子字符串。(包start不包end)
System.out.println("substring:"+s.substring(4,8));//owor
System.out.println("substring:"+s.substring(0,s.length()));//helloworld
}
}