分享

String的截取功能

 樱花梦_张艺馨 2016-10-29

package ss1;
/**
 *String类的获取功能
 *public int length()   返回此字符串的长度。
 *public char charAt(int index) 返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。
 *public int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。
 *public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引。
 *public int indexOf(int ch, int fromIndex)返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
 *public int indexOf(String str, int fromIndex)返回指定子字符串在此字符串中第一次出现处的索引,
 *public String substring(int beginIndex)返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
 *public String substring(int beginIndex,int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。
 */
public class Menu {
 public static void main(String[] args) {
  
  String s = "helloworld";
  System.out.println("s.length : "+s.length());
  
  System.out.println("s.charAt : "+s.charAt(2));

  System.out.println("s.indexOf : "+s.indexOf('h'));
  System.out.println("s.indexOf : "+s.indexOf('w'));
  System.out.println("s.indexOf : "+s.indexOf('K'));
  
  System.out.println("s.indexOf : "+s.indexOf("he"));
  System.out.println("s.indexOf : "+s.indexOf("wo"));
  System.out.println("s.indexOf : "+s.indexOf("hw"));
  
  System.out.println("s.indexOf : "+s.indexOf('l',5));
  
  System.out.println("s.substring : "+s.substring(5));
  System.out.println("s.substring : "+s.substring(5,7));
 }
 
}

结果:
s.length : 10
s.charAt : l
s.indexOf : 0
s.indexOf : 5
s.indexOf : -1
s.indexOf : 0
s.indexOf : 5
s.indexOf : -1
s.indexOf : 8
s.substring : world
s.substring : wo

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多