----------------------android培训、java培训、期待与您交流! ----------------------
一个字符串对象包含一个通常用做文本处理的字符序列, 在Java中提供了String类来支持对字符串的建立和处理,该类包含的方法个数高达50个,并且包含10余个的构造器。
一些常用的方法罗列在下面的类定义中:
public final class String
{
public char charAt();
public boolean endswith(String suffix);
public boolean equals(Object object);
public int indexOf(char ch);
public int indexOf(char ch,int start);
public int lastindexOf(char ch);
public int lastindexOf(char ch,int start);
public int lastindexOf(String str);
public int lastindexOf(String str,int start);
public int length();
public String replace(char ch,char ch2);
public boolean startWith(String prefix,int Start);
public String();
public String(char [] chars);
public String(char [] chars,int start,int len);
public String subString(int start);
public String subString(int start,int stop);
public char[] toCharArray();
public String toLowerCase();
public String toUpperCase();
public String trim();
}
尽管串是对象而不是基本类型,但它们在某些方面却与基本类型相似。系统对字符串文字(如”blue”和”’Tis so.”)的识别和处理与对数字文字(如8388608和3.14159)的识别和处理是一样的。然而,串文字可以赋值给串引用,如:String color = “blue”;
因为文字是惟一的,任何指向”blue”的引用与 color引用进行相同测试时,将返回真值。 正如数字类型一样,串是可通过运算符进行处理的唯一的对象类,其他类对象只能由类中的方法进行操作处理。
除此之外,String类还定义了两个并置运算符+和+=,这些将在下面的例子中加以说明。
例1 测试String类
public class Ex0108
{
public static void main(String[] args)
{
String s = "ABCDEFG";
System.out.println("S = \"" + s + "\"");
s = s + "HIJK";
System.out.println("S = \"" + s + "\"");
s = s + "LMNOP";
System.out.println("S = \"" + s + "\"");
System.out.println("S.length()=" + s.length());
System.out.println("S.charAt(6)" + s.charAt(6));
System.out.println("S.indexOf('G')" + s.indexOf('G'));
System.out.println("S.indexOf('Z')" + s.indexOf('z'));
System.out.println("S.indexOf('G',8)" + s.indexOf('G',8));
System.out.println("S.indexOf(\"GHIJ\")" + s.indexOf("GHIJ"));
if(s.startsWith("DE"))
System.out.println("s.startsWith(\"DE\")");
else
System.out.println("s dose not start with \"DE\"");
if(s.startsWith("DE",3))
System.out.println("s.startsWith(\"DE\",3)");
else
System.out.println("s dose not start with \"DE\" after 3 chars");
if(s.endsWith("IJK"))
System.out.println("s.endsWith(\"IJK\")");
else
System.out.println("s dose not start with \"IJK\"");
if(s.endsWith("NOP"))
System.out.println("s.endsWith(\"NOP\")");
else
System.out.println("s dose not start with \"NOP\"");
s+="DABBADABBADO";
System.out.println("s = \""+s+"\"");
s = s.replace('B','T');
System.out.println("s = \""+s+"\"");
s = s.substring(7,10);
System.out.println("s = \""+s+"\"");
s = s.toLowerCase();
System.out.println("s = \""+s+"\"");
s = " W XY Z ";
System.out.println("s = \""+s+"\"");
System.out.println("s.length() ="+s.length());
s = s.trim();
System.out.println("s = \""+s+"\"");
System.out.println("s.length() ="+s.length());
}
}
该程序的输出为:
注意,String 对象是不能改变的,所以改变一个字符串的唯一办法是给它赋予一个方法的返回值,例如方法repalce(char,char)。
Java需要计算操作系统的支持才能维持一个字符串文字池,使得在任何运行环境下仅有一个相同的字符串文字在使用。这类似于数字,只能有一个27。这个非同寻常的要求是为了保证对字符串文字实施正确的相等运算符==所提出的。
例2 测试S tring文字的唯一性
public class Ex0109
{
public static void main(String [] args)
{
String s1 = "ABCDEFG";
System.out.println("s1 = \"" + s1 +"\"");
System.out.println("(s1 == \"ABCDEFG\")=" + (s1=="ABCDEFG"));
System.out.println("(s1 == \"ABCD\"+\"EFG\")="+(s1 == "ABCD"+"EFG"));
String s2 = "ABCDEFG"; //makes s2 a synonym for s1
System.out.println("s2 = \"" + s2 + "\"");
System.out.println("(s1== s2)=" +(s1 == s2));
s2 = new String("ABCDEFG"); //now s2 refers to separate object
System.out.println("s2 = \"" +s2+"\"");
System.out.println("(s1 ==s2)=" +(s1 == s2));
System.out.println("s1.equals(s2)="+s1.equals(s2));
}
}
该程序的结果为:
上例说明仅有一个字符串文字”ABCDEFG”,因此,当两个不同的引用(即引用具有不同的名字)均被赋值成该文字时,它们必须相同。也就是说,相等运算符==应为true。但请注意两个不同的字符串可以有相同的文字值,在这种情况下,相等运算符的返回值为假,而equals()方法仍然为真。
这些特性使得String类与其他类不同:
1)String对象是不可改变对象,这意味着它们的值是不能改变的,它们是只读对象,因为没有为该类定义mutator方法。
2)String文字均由操作系统的字符串池所维持。
3)String类中定义了两个特定的运算符+和+=。
4)String类对象的字符长度由length()方法获得,这与数组对象所用的length成员或集合对象所采用的size()方法不同。
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima