字符串--字符串的访问

来源:互联网 发布:重生之古董商淘宝之旅 编辑:程序博客网 时间:2024/06/05 21:13

 1.类String中提供了length( )、charAt( )、indexOf( )、lastIndexOf( )、getChars( )、getBytes( )、toCharArray( )等方法。

  
public int length() 此方法返回字符串的字符个数
   public char charAt(int index) 此方法返回字符串中index位置上的字符,其中index 值的 范围是0~length-1
   public int indexOf(int ch)
    public lastIndexOf(in ch)
  
  返回字符ch在字符串中出现的第一个和最后一个的位置
   public int indexOf(String str)
    public int lastIndexOf(String str)
  返回子串str中第一个字符在字符串中出现的第一个和最后一个的位置
   public int indexOf(int ch,int fromIndex)
    public lastIndexOf(in ch ,int fromIndex)
  返回字符ch在字符串中位置fromIndex以后出现的第一个和最后一个的位置
   public int indexOf(String str,int fromIndex)
    public int lastIndexOf(String str,int fromIndex)
  返回子串str中的第一个字符在字符串中位置fromIndex后出现的第一个和最后一个的位置。
   public void getchars(int srcbegin,int end ,char buf[],int dstbegin)
   srcbegin 为要提取的第一个字符在源串中的位置, end为要提取的最后一个字符在源串中的位置,字符数组buf[]存放目的字符串,    dstbegin 为提取的字符串在目的串中的起始位置。
  public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin)
  参数及用法同上,只是串中的字符均用8位表示。

 
2.类StringBuffer提供了 length( )、charAt( )、getChars( )、capacity()等方法。

  方法capacity()用来得到字符串缓冲区的容量,它与方法length()所返回的值通常是不同的。

原创粉丝点击