Java基本的程式設計結構(二)

來源:互聯網
上載者:User
(六)字串
1.在這裡需要指出的是它是一個對象,不是一個字元數組

2.他是不可變的,沒有append()或reverse()

3.要用雙引號,串連時用+

4.給出一些常用函數,相關的可查看API文檔
toString();
int length() -- number of chars
char charAt(int index) -- char at given 0-based index
int indexOf(char c) -- first occurrence of char, or -1
int indexOf(String s)
boolean equals(Object) -- test if two strings have the same characters
boolean equalsIgnoreCase(Object) -- as above, but ignoring case
String toLowerCase() -- return a new String, lowercase
String substring(int begin, int end) -- return a new String made of the begin..end-1 substring from the original

5.初始化
 s1 = new String(“This is some java String”);
 s1 = new String(“This is some java String”);

6.例子
String a = "hello";    // allocate 2 String objects
  String b = "there";
  String c = a;    // point to same String as a – fine

  int len = a.length();    // 5
  String d = a + " " + b;    // "hello there"
   
  int find = d.indexOf("there");    // find: 6
  String sub = d.substring(6, 11);    // extract: "there“

  sub == b;        // false (== compares pointers)
  sub.equals(b);    // true (a "deep" comparison)

String a = "hello";    // allocate 2 String objects
  String b = "there";
  String c = a;    // point to same String as a – fine

  int len = a.length();    // 5
  String d = a + " " + b;    // "hello there"
   
  int find = d.indexOf("there");    // find: 6
  String sub = d.substring(6, 11);    // extract: "there“

  sub == b;        // false (== compares pointers)
 
Use the equals method;
    s.equals(t) // returns true if the strings s and t are equal, false otherwise.
s and t can be string variables or string constants. "Hello".equals(command) // OK
To test if two strings are identical except for the upper/lowercase letter distinction
      “Hello".equalsIgnoreCase("hello")

7.比較時一般不用==
String greeting = "Hello";    
    if (greeting == "Hello") . . . // probably true
    if (greeting.substring(0, 4) == "Hell") . . . // probably false

8.StringBuffer
不同於String,
append() method- Lets you add characters to the end of a StringBuffer object

insert() method- Lets you add characters at a specified location within a StringBuffer object

setCharAt() method- Use to alter just one character in a StringBuffer

例子:
StringBuffer buff = new StringBuffer();
        for (int i=0; i<100; i++) {
            buff.append(<some thing>);
            // efficient append
        }
        String result = buff.toString();
        // make a String once done with appending

9.StringTokenizer
可以根據某些標記(token),來分割String
例子:
String testString = “one-two-three”;
StringTokenizer st = new StringTokenizer(testString,”-");
while (st.hasMoreTokens()) {
       String nextName = st.nextToken();
       System.out.println(nextName);
}
Result:
one
two
three

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.