ºìÁªLinuxÃÅ»§
Linux°ïÖú

̽Ë÷String Àà

·¢²¼Ê±¼ä:2006-05-03 14:42:20À´Ô´:ºìÁª×÷Õß:reing
¾¡¹ÜString ÀཫÔÚ±¾ÊéµÄµÚ2²¿·ÖÉîÈëµØÑо¿£¬µ«ÒòΪÎÒÃǽ«ÔÚµÚ1²¿·ÖĩβµÄһЩÀý×Ó³ÌÐòÖÐʹÓÃ×Ö·û´®£¬Òò´Ë£¬ÏÖÔÚÓ¦¸Ã¶ÔËü×öÒ»¸ö¼òµ¥µÄ̽Ë÷¡£String ÀàÊÇJava Àà¿âÖÐ×î³£ÓõÄÀ࣬ÆäÖÐ×îÃ÷ÏÔµÄÔ­ÒòÊÇ×Ö·û´®ÔÚ±à³ÌÓïÑÔÖÐÊǺÜÖØÒªµÄ²¿·Ö¡£

ÓйØ×Ö·û´®µÄ×îÖØÒªÒ»µãÊÇ£¬Äã´´½¨µÄÿһ¸ö×Ö·û´®Êµ¼ÊÉ϶¼ÊÇString ÀàÐ͵ÄÒ»¸ö¶ÔÏ󣬼´Ê¹ÊÇ×Ö·û´®³£Á¿Êµ¼ÊÉÏÒ²ÊÇString ¶ÔÏó¡£System.out.println("This is a String£¬too");

×Ö·û´®¡°This is a String£¬too ¡±ÊÇÒ»¸ö×Ö·û´®³£Êý¡£ÐҺã¬Java ´¦Àí×Ö·û´®³£ÊýºÍÆäËû¼ÆËã»úÓïÑÔ´¦Àí¡°Õý³£¡±µÄ×Ö·û´®µÄ·½·¨Ò»Ñù£¬Òò´ËÄã²»±Øµ£ÐÄÕâ¸ö¡£

×Ö·û´®µÄÁíÒ»¸öÌصãÊÇ£¬String ÀàÐ͵ĶÔÏóÊDz»¿É¸Ä±äµÄ£»Ò»µ©´´½¨ÁËÒ»¸ö×Ö·û´®¶ÔÏó£¬ËüµÄÄÚÈÝÊDz»Äܱ»¸Ä±äµÄ¡£Õâ¿´ÆðÀ´ÊÇÒ»¸öÑϸñµÄÏÞÖÆ£¬µ«Êµ¼ÊÉϲ»ÊÇ£¬ÒòΪÕâÓÐÁ½¸öÔ­Òò£º

?
Èç¹ûÄãÐèÒª¸Ä±äÒ»¸ö×Ö·û´®£¬Äã¿ÉÒÔ´´½¨Ò»¸öеÄ×Ö·û´®£¬ÆäÖаüº¬Ð޸ĺóµÄ×Ö·û´®¼´¿É¡£
?
Java ¶¨ÒåÁËÒ»¸öºÍString ÀàͬµÈµÄÀà½ÐStringBuffer £¬ËüÔÊÐí×Ö·û´®¸Ä±ä£¬Òò´ËËùÓÐÕý³£µÄ×Ö·û´®²Ù×÷ÔÚJava Öл¹ÊÇ¿ÉÓõģ¨StringBuffer ÔÚ±¾ÊéµÄµÚ2²¿·ÖÃèÊö£©¡£
×Ö·û´®¿ÉÒÔͨ¹ý¶àÖÖ·½·¨¹¹Ôì¡£×îÈÝÒ×µÄÒ»ÖÖÓÃÈçϵÄÓï¾ä£º

String myString = "this is a test";

Ò»µ©Äã´´½¨ÁËÒ»¸ö×Ö·û´®¶ÔÏó£¬Äã¿ÉÒÔÔÚÈκÎÔÊÐí×Ö·û´®µÄµØ·½Ê¹ÓÃËü£¬ÀýÈçÏÂÃæÕâÌõÓï¾äÏÔʾmyString £º

System.out.println(myString);

Java ¶¨ÒåÁËÒ»¸öString ¶ÔÏóµÄÔËËã·û£º¡°+¡±¡£ËüÓÃÀ´Á¬½ÓÁ½¸ö×Ö·û´®¡£ÀýÈ磬ÏÂÃæÕâÌõÓï¾ä£º

String myString = "I" + " like " + "Java.";

µÄ½á¹ûÊÇmyString °üº¬¡°I like Java.¡±ÏÂÃæµÄ³ÌÐò±íÃ÷ÁËÇ°ÃæµÄ¸ÅÄ

// Demonstrating Strings.
class StringDemo {

public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1 + " and " + strOb2;

System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);

}
}

¸Ã³ÌÐò²úÉúµÄÊä³öÈçÏÂËùʾ£º

First String
Second String
First String and Second String

String Àà°üº¬Ðí¶à²Ù×÷×Ö·û´®µÄ·½·¨¡£ÀýÈçÏÂÃæ¾ÍÊÇÆäÖÐһЩ¡£Äã¿ÉÒÔÓÃequals() À´¼ìÑéÁ½¸ö×Ö·û´®ÊÇ·ñÏàµÈ¡£Äã¿ÉÒÔµ÷Ó÷½·¨length() À´»ñµÃÒ»¸ö×Ö·û´®µÄ³¤¶È¡£Äã¿ÉÒÔµ÷ÓÃcharAt() À´»ñµÃÒ»¸ö×Ö·û´®Ö¸¶¨Ë÷ÒýµÄ×Ö·û¡£ÕâÈý¸ö·½·¨µÄͨÓøñʽÈçÏÂËùʾ£º

boolean equals(String object)
int length( )
char charAt(int index)

ÏÂÃæµÄ³ÌÐòʾÀýÁËÕâЩ·½·¨£º

// Demonstrating some String methods.
class StringDemo2 {

public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;

System.out.println("Length of strOb1: " +
strOb1.length());

System.out.println("Char at index 3 in strOb1: " +
strOb1.charAt(3));

if(strOb1.equals(strOb2)) System.out.println("strOb1 == strOb2"); else System.out.println("strOb1 != strOb2");

if(strOb1.equals(strOb3)) System.out.println("strOb1 == strOb3"); else System.out.println("strOb1 != strOb3"); }}

¸Ã³ÌÐò²úÉúÈçϵÄÊä³ö£º

Length of strOb1: 12Char at index 3 in strOb1: s strOb1 != strOb2strOb1 == strOb3

µ±È»£¬ÓëÆäËû¶ÔÏóÀàÐÍÒ»Ñù£¬strings Ò²¿ÉÒÔ×é³ÉÊý×飬ÀýÈ磺

// Demonstrate String arrays.class StringDemo3 {public static void main(String args[]) { String str[] = { "one"£¬"two"£¬"three" };

for(int i=0; iSystem.out.println("str[" + i + "]: " +
str[i]);
}
}

ÏÂÃæÊǸóÌÐò²úÉúµÄÊä³ö£º

str[0]: one
str[1]: two
str[2]: three

ÔÚϽÚÀïÄ㽫¿´µ½£¬×Ö·û´®Êý×éÔÚÐí¶àJava ³ÌÐòÖÐÆðÖØÒªµÄ×÷Óá£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ