当前位置: 技术问答>java相关
java中字符截取的问题!(在线等待)
来源: 互联网 发布时间:2015-06-19
本文导语: 小弟初学,问一个简单问题。java中怎么截取字符串。 例如:String s = "abcde,fghi";问怎么把abcde付给一个字符串而把fghi付给令一个字符串。用getChars只能给一个字符数组啊! | String s = "abcde,...
小弟初学,问一个简单问题。java中怎么截取字符串。
例如:String s = "abcde,fghi";问怎么把abcde付给一个字符串而把fghi付给令一个字符串。用getChars只能给一个字符数组啊!
例如:String s = "abcde,fghi";问怎么把abcde付给一个字符串而把fghi付给令一个字符串。用getChars只能给一个字符数组啊!
|
String s = "abcde,fghi";
int i = s.indexOf(",");
String s1 = s.substring(0,i);
String s2 = s.substring(i);
System.out.println(s1);
System.out.println(s2);
int i = s.indexOf(",");
String s1 = s.substring(0,i);
String s2 = s.substring(i);
System.out.println(s1);
System.out.println(s2);
|
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.
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.