1.String构造函数
默认空字符串构造函数:
String a = new String();
带初始化值的构造函数:
1).char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
2).char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String s = new String(chars, 2, 3);
该例子用字符cde初始化s
3).char c[] = {'J', 'a', 'v', 'a'};
String s1 = new String(c);
String s2 = new String(s1);
2.字符串长度
char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
System.out.println(s.length());
3.字符串转换 toString()
public class Test{
int m;
Test(int n){
m = n;
}
public String toString(){ //返回一个字符串
return "number is "+m;
}
public static void main(String args[]){
Test a = new Test(10);
System.out.println(a.toString());
}
}
4.字符截取
1)charAt
char n;
n = "abc".charAt(1);
2)getChars
一次截取多个字符
class getCharsDemo {
public static void main(String args[]) {
String s = "This is a demo of the getChars method.";
int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);
}
}
5.字符串比较
1)equals( )和 equalsIgnoreCase( )
使用equals( )方法比较两个字符串是否相等,equalsIgnoreCase( )方法比较时不分大小写。
2)regionMatches( )
regionMatches( )方法将一个字符串中指定的区间和另一字符串中指定的区间进行比较。它的重载形式允许在比较时忽略大小写。下面给出这两种方法的一般形式:
boolean regionMatches(int startIndex, String str2,int str2StartIndex, int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex, String str2,int str2StartIndex, int numChars)
对于这两种形式,startIndex指定了调用字符串(String)对象内区间开始的下标。用于比较的字符串(String)由str2指定的。在str2内,开始比较区间的下标由str2StartIndex指定。用于比较的子字符串的长度在numChars中。在第二种方案中,如果ignoreCase是true,字符的大小写被忽略。否则,大小写是有意义的。
3)startsWith( )和endsWith( )
字符串(String)定义两个例程,它们或多或少是regionMatches( )方法的特殊形式。startsWith( )方法判断一个给定的字符串(String)是否从一个指定的字符串开始。相反地,endsWith( )方法判断所讨论的字符串(String)是否是以一个指定的字符串结尾。它们具有如下的一般形式:
boolean startsWith(String str)
boolean endsWith(String str)
这里,str是被测试的字符串(String)。如果字符串匹配,返回true。否则返回false。
例如:
"Foobar".endsWith("bar")
和
"Foobar".startsWith("Foo")
都是true。
下面给出startsWith( )方法的第二种形式。这种形式允许指定一个起始点:
boolean startsWith(String str, int startIndex)
这里,startIndex指定了调用字符串开始搜索的下标。例如,
"Foobar".startsWith("bar", 3)
返回true。
4)equals( )与==的比较
理解equals( )方法和==运算符执行的是两个不同的操作是重要的。如同刚才解释的那样,equals( )方法比较字符串(String)对象中的字符。而==运算符比较两个对象引用看它们是否引用相同的实例。下面的程序说明了两个不同的字符串(String)对象是如何能够包含相同字符的,但同时这些对象引用是不相等的:
// equals() vs ==
class EqualsNotEqualTo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = new String(s1);
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}
}
变量s1指向由“Hello”创建的字符串(String)实例。s2所指的的对象是以s1作为初始化而创建的。因此这两个字符串(String)对象的内容是一样的。但它们是不同的对象,这就意味着s1和s2没有指向同一的对象,因此它们是不==的,上面例子的结果如下:
Hello equals Hello -> true
Hello == Hello -> false
5)compareTo( )
通常,仅仅知道两个字符串是否相同是不够的。对于排序应用来说,必须知道一个字符串是大于、等于还是小于另一个。一个字符串小于另一个指的是它在字典中先出现。而一个字符串大于另一个指的是它在字典中后出现。字符串(String)的compareTo( )方法实现了这种功能。它的一般形式如下:
nt compareTo(String str)
这里str是与调用字符串(String)比较的字符串(String)。比较的结果返回并被解释
如表13-1所示:
值 | 含义 |
小于0 | 调用字符串小于str |
大于0 | 调用字符串大于str |
等于0 | 调用字符串相等 |
class SortString {
static String arr[] = { "Now", "is", "the", "time", "for", "all", "good",
"men", "to", "come", "to", "the", "aid", "of", "their", "country" };
public static void main(String args[]) {
for (int j = 0; j < arr.length; j++) {
for (int i = j + 1; i < arr.length; i++) {
if (arr[i].compareTo(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
System.out.println(arr[j]);
}
}
}
正如从这个例子的输出所能看到的,compareTo( )方法区分单词的大小写。单词“Now”因为是以大写字母开始的而出现在其他单词的前面,这意味着它在ASCII字符集中具有更小的值。如果想在比较两个字符串时,忽略大小写,可以使用如下的compareToIgnoreCase( )方法:
int compareToIgnoreCase(String str)
除了忽略大小写之外,该方法的返回值与compareTo( )方法相同。该方法是在Java 2中新增加的。可以在前面的程序中换成这个方法。这样做了之后,“Now”将不再是第一个输出了。
6)搜索字符串
String类提供了两个方法,允许在字符串中搜索指定的字符或子字符串:
· indexOf( ) 搜索字符或子字符串首次出现。
· lastIndexOf( ) 搜索字符或子字符串的最后一次出现。
这两种方法被几种不同的方法重载。在所有这些情况下,方法返回字符或子字符串被发现的位置的下标,当搜索失败时,返回-1。
搜索字符首次出现用
int indexOf(int ch)
搜索字符最后一次出现用
int lastIndexOf(int ch)
这里ch是被查找的字符。
搜索子字符串首次或最后一次出现用
int indexOf(String str)
int lastIndexOf(String str)
这里子字符串由str指定
可以使用如下这些形式指定搜索的起始点:
int indexOf(int ch, int startIndex)
int lastIndexOf(int ch, int startIndex)
int indexOf(String str, int startIndex)
int lastIndexOf(String str, int startIndex)
7)修改字符串
★substring( )
利用substring( )方法可以截取子字符串,它有两种形式。其中第一种形式如下:
String substring(int startIndex)
这里startIndex指定了子字符串开始的下标。这种形式返回一个从startIndex开始到调用字符串结束的子字符串的拷贝。
substring( )方法的第二种形式允许指定子字符串的开始和结束下标:
String substring(int startIndex, int endIndex)
这里startIndex指定开始下标,endIndex指定结束下标。返回的字符串包括从开始下标直到结束下标的所有字符,但不包括结束下标对应的字符。
★replace( )
replace( )方法用另一个字符代替调用字符串中一个字符的所有具体值。它具有如下的一般形式:
String replace(char original, char replacement)
这里original指定被由replacement指定的字符所代替的字符,返回得到的字符串。例如:
String s = "Hello".replace('l', 'w');
将字符串“Hewwo”赋给s。
★trim( )
trim( )方法返回一个调用字符串的拷贝,该字符串是将位于调用字符串前面和后面的空白符删除后的剩余部分。它的一般形式如下:
String trim( )
这里是一个例子:
String s = " Hello World ".trim();
将字符串“Hello World”赋给s。