怎么样得到一个字符串的后面几个字符?
怎么样得到一个字符串的后面几个字符? 问题点数:100、回复次数:7Top
1 楼linus_lee(会游泳的鱼)回复于 2005-08-03 11:39:05 得分 0
subString(str,m,str.length)Top
2 楼shenpipi(皮皮)回复于 2005-08-03 11:39:37 得分 0
要是想要后面的几个字符组成的字符串用,subString,要单个的字符,用charAtTop
3 楼rower203(华仔)回复于 2005-08-03 11:43:44 得分 0
String str = "adfasdf";
int n = 3;
System.out.println(str.substring(str.length() - n));Top
4 楼masse(当午 http://blog.sina.com.cn/xukf)回复于 2005-08-03 11:44:06 得分 0
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
Top
5 楼masse(当午 http://blog.sina.com.cn/xukf)回复于 2005-08-03 11:45:41 得分 0
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); //heheTop
6 楼interhanchi(on the Java Road)回复于 2005-08-03 12:00:03 得分 0
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.
Top
7 楼laughsmile(海边的星空)回复于 2005-08-03 12:51:48 得分 0
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);
}
Top




