要是想要后面的几个字符组成的字符串用,subString,要单个的字符,用charAt

解决方案 »

  1.   

    String str = "adfasdf";
         int n = 3;
         System.out.println(str.substring(str.length() - n));
      

  2.   

    String src = "abchehehehe";
    String s = "abc";// 得到src中“abc”之后的所有字符串
    String result = src.substring(src.indexOf(s)+s.length);// hehehehe// 得到src中“abc”之后的长度为4的字符串String result = src.substring(src.indexOf(s)+s.length , src.indexOf(s)+s.length + 4); //hehe
      

  3.   

    String src = "abchehehehe";
    String s = "abc";
    int index = src.indexOf(s)+s.length; // "abc"后的位置// 得到src中“abc”之后的所有字符串
    String result = src.substring(index);// hehehehe// 得到src中“abc”之后的长度为4的字符串
    String result1 = src.substring(index,index+4); //hehe
      

  4.   

    1
    substring
    public String substring(int beginIndex)
    Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. 
    Examples:  "unhappy".substring(2) returns "happy"
     "Harbison".substring(3) returns "bison"
     "emptiness".substring(9) returns "" (an empty string)
     Parameters:
    beginIndex - the beginning index, inclusive. 
    Returns:
    the specified substring. 
    Throws: 
    IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.2
    substring
    public String substring(int beginIndex,
                            int endIndex)
    Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. Examples:  "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"
     Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive. 
    Returns:
    the specified substring. 
    Throws: 
    IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
      

  5.   

    public String last(String str,int count){
        if(str == null)
            return null;
        if(count <= 0)   
            return null;
        if(str.length()<=count)
            return str;
        return str.substring(str.length()-count);
    }