Java basic programming structure (2)

Source: Internet
Author: User
(6) String
1. here we need to point out that it is an object rather than a character array.

2. It is immutable, with no append () or reverse ()

3. Use double quotation marks and + for connection

4. provide some common functions. For more information, see the API documentation.
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 inclusignorecase (object) -- as abve, 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. Initialization
S1 = new string ("this is some java string ");
S1 = new string ("this is some java string ");

6. Example
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 limits t for the Upper/lowercase letter distinction
"Hello". equalsignorecase ("hello ")

7. Generally, = is not used for comparison.
String greeting = "hello ";
If (greeting = "hello")... // probably true
If (greeting. substring (0, 4) = "hell")... // probably false

8. stringbuffer
Different from 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

Example:
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
You can use some tokens to separate strings.
Example:
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.