Java Learning Note 05--string.

Source: Internet
Author: User

Java Learning Note 05--string.

First, the String class

A string of literal symbols made up of characters are called strings. in Java, a string is not just an array of characters, but is an instance of the string class that can be constructed using the string class.

Each character of a string is constructed using Unicode characters.

Several methods on the Sring object:

Length () Gets the lengths of the strings

Equals () determines whether the characters in the source string are equal to the characters in the specified string

toLowerCase () converts the English characters in a string to lowercase

toUpperCase () converts the English characters in the string to uppercase

The nature of a string is made up of character arrays, so when you declare a string using the String class, the string has the properties of the array index.

char charAt (int index) returns the character at the specified index

int indexOf (int ch) returns the first index position found by the specified character

int indexOf (String str) returns the first index position found for the specified string

int lastIndexOf (int ch) returns the last index position found for the specified character

string substring (int beginindex) extracts substrings at the specified index to the end of the string

string substring (int beginindex, int endIndex) takes out the specified index range substring ( including beginindex, not including EndIndex)

Char[] ToCharArray () converts a string to a character array

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. String str = "Hello itmyhome";
  4. For (int i=0;i<str.length (); i++) {
  5. System.out.print (Str.charat (i));
  6. }
  7. System.out.println ();
  8. System.out.println ("First O index Position:" +str.indexof ("O"));
  9. System.out.println ("Last O index Position:" +str.lastindexof ("O"));
  10. System.out.println ("intercept from the 6th character to the tail:" +str.substring (6));
  11. System.out.println ("intercept from 6th to 8th characters:" +str.substring (6, 8));
  12. char charstr[] = Str.tochararray ();
  13. For (int i = 0; i < charstr.length; i++) {
  14. System.out.print (Charstr[i]);
  15. }
  16. }
  17. }

Execution Result:

[Java]View Plaincopy
    1. Hello Itmyhome
    2. First O index position: 4
    3. Last O index position: one
    4. Intercept from the first 6 characters to the end: Itmyhome
    5. Intercept from 6 to 8 characters: it
    6. Hello Itmyhome


When building a string object, you can use a character array to build it, in addition to specifying string constants directly after = using "":

[Java]View Plaincopy
    1. Char name[] = {' i ',' t ',' m ',' y ',' h ',' o ',' m ',' e '};
    2. string s = new string (name);


Second, immutable string

A very important rule to use strings in Java is to remember that once a string object is configured, its contents are fixed and immutable.

In Java, use "=" To assign a string object to a reference name, meaning to change the object that the name refers to, and if the original referenced string object has no other name to reference it, it will be recycled by Java's garbage collection mechanism when appropriate.

In Java execution, a string pool is maintained, and for some string objects that can be shared, there is now a string pool looking for the same string content (the same character), and if so, return directly instead of creating a new string object directly. To reduce the memory consumption. If you declare it in your program in the following way, you are actually pointing to the same string object:

[Java]View Plaincopy
    1. String str1 = "Itmyhome";
    2. String str2 = "Itmyhome";
    3. System.out.println (STR1==STR2);


When you use "" in a program directly to include a string, the string is in the string pool.

StringBuilder class

Reference http://blog.csdn.net/itmyhome/article/details/7232340

Separating strings

You can use the split () string to separate strings

Note the question : ".", "|", "? "is an escape character and must be added" \ \ "

[Java]View Plaincopy
    1. String s1[] = str.split ("\ \");
    2. String s2[] = Str.split ("\\|");
    3. String s3[] = str.split ("\ \");


If you have more than one delimiter in a string, you can use the ' | ' As a hyphen.

[Java]View Plaincopy
    1. String str = "Welcome?to!itmyhome Bolg";
    2. String s[] = Str.split ("\\?|!");
    3. For (int i = 0; i < s.length; i++) {
    4. System.out.println (S[i]);
    5. }

To? And! As a separator character.

Java Learning Note 05--string.

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.