当前位置: 技术问答>java相关
请教两个字符串处理问题?
来源: 互联网 发布时间:2015-04-26
本文导语: 1、怎样从一段字符串中提取指定位置的字符串?例如从"string"中取出或删除"tri"。 2、怎样分解字符串?例如将"test string"分解为"test"和"string"。 ps:请问哪里可以下载到java的类库说明大全? | 1. String a =...
1、怎样从一段字符串中提取指定位置的字符串?例如从"string"中取出或删除"tri"。
2、怎样分解字符串?例如将"test string"分解为"test"和"string"。
ps:请问哪里可以下载到java的类库说明大全?
2、怎样分解字符串?例如将"test string"分解为"test"和"string"。
ps:请问哪里可以下载到java的类库说明大全?
|
1.
String a = "string";
int index = a.indexOf("tri");
a = a.substring(index, index + "tri".length());
删除的话可以用
a = a.substring(0,index) + a.substring(index+"tri".length());
2.
StringTokenizer st = new StringTokenizer("test string"," ");
while(st.hasNextToken()) {
System.out.println(st.nextToken());
}
3.
http://java.sun.com
String a = "string";
int index = a.indexOf("tri");
a = a.substring(index, index + "tri".length());
删除的话可以用
a = a.substring(0,index) + a.substring(index+"tri".length());
2.
StringTokenizer st = new StringTokenizer("test string"," ");
while(st.hasNextToken()) {
System.out.println(st.nextToken());
}
3.
http://java.sun.com
|
1. String s = "string";
s = s.substring(1,4);
2. 同1, 用substring方法.
这是用法:
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.
s = s.substring(1,4);
2. 同1, 用substring方法.
这是用法:
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.
|
1.删除与提取都可以用substring(start,length),判断位置可以用indexof("tri")
也可以利用StringBuffer类,这个类里有insert和remove的方法,据讲效率高
分解可以用indexof(" ")来断分解位置,然后再利用substring
2.类库大全jdk自带,也可以去买书
也可以利用StringBuffer类,这个类里有insert和remove的方法,据讲效率高
分解可以用indexof(" ")来断分解位置,然后再利用substring
2.类库大全jdk自带,也可以去买书