java.lang.Stringpublic 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.
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
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
String str = "adfasdf"; int n = 3; System.out.println(str.substring(str.length() - n));
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.
精髓在这3句: String str = "adfasdf"; int n = 3; System.out.println(str.substring(str.length() - n));
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.
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
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
int n = 3;
System.out.println(str.substring(str.length() - n));
{
if(str == null)
return null;
if(count <= 0)
return null;
if(str.length()<=count)
return str;
return str.substring(str.length()-count);
}
假如需要连接使用
那么在使用的过程中需要注意,因为String类型比较特殊。
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);
}
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.
String str = "adfasdf";
int n = 3;
System.out.println(str.substring(str.length() - n));
str.substring(str.length() - n,str.length());
字符串从指定字符位置开始,且具有指定长度。
与substring(str.length()-n)是等同的.
与substring(str.length()-n)是等同的.