Strings in Java (string)

Source: Internet
Author: User

First, the basic concept:

A string is a sequence of a series of strings. In the Java language, a string is a series of characters enclosed in a pair of double quotation marks "". For example "Hello", "Hello". From the point of view of an array, a string can be considered to be a single character array.

The strings used in the program can be divided into two main categories:

One is to create a string variable that is not modified or changed after it is created, and it is often used to do comparisons, searches, and so on, which are string objects of type

Another is a string variable that is allowed to be modified after creation, and a string variable that needs to be frequently added, inserted, modified, and so on, is generally placed in the object of the StringBuilder class.

Second, the creation of a string

 Format one:

String variable name;

Variable name =new string ("string");

String str;//declares a string that references the variable str, at which time the value of STR is NULL

str = new String ("HelloWorld");//Allocates space in heap memory and points STR to the first address of the string

Format two:

String str = new String ("HelloWorld");//At the time of creation and initialization;

Format three:

String str = "HelloWorld";

Strings are reference scalars, so they are stored in the same way as arrays. Between two strings can be concatenated with the "+" symbol, and a number is added to a string as well as a string. It will be a newly created space in the system after it is connected.

Iv. common methods of string classes

  1, public int length ();//Returns the length of the string

2. Public boolean equals (Object anobject);//compares the given string to the current string, returns True if two characters are equal, otherwise returns false

3, public string substring (int beginindex);//String that returns the strings starting with Beginindex

4, public string substring (int beginindex,int endIndex);//return string starting from Beginindex, Endinedx end of the word string

5, public char charAt (int index);//returns the character at the specified position of index

6, public int indexOf (string str);//Returns the position of the first occurrence in the string

7, public int CompareTo (string anotherstring);//If the character calling the method is greater than the argument string, returns a value greater than 0, or 0 if equal; Returns a value less than 0 if it is less than the argument string

8. public string Replace (char OldChar, char newchar);//Replace All OldChar characters in the string with Newchar characters

9, public string trim ();//Remove the string from the leading and trailing spaces

10, public string toUpperCase ();//converts all characters of a string to uppercase character

11, public string Tolowercae ();//Convert all characters of string to lowercase character

Strings in Java (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.