Java Common class-string

Source: Internet
Author: User

Java String Class

Strings are widely used in Java programming, where strings belong to objects, and Java provides a string class to create and manipulate strings.

Create a string

The simplest way to create a string is as follows:

1 String greeting = "Xiao Meng";

When you encounter a string constant in your code, the value here is " small ," and the compiler uses that value to create a string object.

As with other objects, you can use keywords and construction methods to create a String object.

The string class has 11 constructor methods that provide different parameters for initializing strings, such as providing a character array parameter:

Stringdemo.java File Code:

1  Public class stringdemo{2     Public Static void Main (String args[]) {3       Char [] Helloarray = {' R ', ' U ', ' n ', ' o ', ' o ', ' B '}; 4       New String (Helloarray);   5       System.out.println (hellostring); 6    }7 }

The results of the above example compilation run as follows:

1 runoob

Note: The string class is immutable, so once you create a string object, its value cannot be changed (see part of the note parsing).

This can be based on the JDK source code to analyze.

The string is actually a char array, and the inner one encapsulates a char array.

And here the char array is final decorated

String length

The method used to get information about an object is called an accessor method.

An accessor method of the string class is the length () method, which returns the number of characters that the string object contains.

After the following code executes, the Len variable equals 13:

Stringdemo.java File Code:

1  Public class Stringdemo {2      Public Static void Main (String args[]) {3         String site = "www.baidu.com"; 4         int len = site.length (); 5         System.out.println ("Baidu URL Length:" + len); 6    }7 }

The results of the above example compilation run as follows:

1 Baidu website length:

Connection string

The string class provides a way to concatenate two strings:

String1.concat (string2);

Returns a new string for the string2 connection string1. You can also use the concat () method on string constants, such as:

"My name is". Concat ("Runoob");

It is more commonly used to concatenate strings using the ' + ' operator, such as:

"Hello," + "Runoob" + "!"

The results are as follows:

"Hello, runoob!."

Here is an example:

Stringdemo.java File Code:

1  Public class Stringdemo {2      Public Static void Main (String args[]) {     3         string string1 = "Baidu URL:";      4         System.out.println ("1," + string1 + "www.Baidu.com");   5     }6 }
The results of the above example compilation run as follows:
1, Baidu website: www.Baidu.com
Creating a formatted string

We know that output formatted numbers can use the printf () and format () methods.

The string class uses the static method format () to return a string object instead of a PrintStream object.

The static method of the String class, format (), can be used to create a reusable format string, not just for one print output at a time.

As shown below:

System.out.printf ("The value of the floating-point variable is" +                  "%f, the value of the integer variable is" +                  "%d, the value of the string variable is" +                  "is%s", Floatvar, Intvar, stringvar);

You can also write this:

= String.Format ("The value of the floating-point variable is" +                   "%f, the value of the integer variable is" +                   "%d, the value of the string variable is" +                   "%s", Floatvar, Intvar, stringvar);
Common interview questions for String classes

String method

The following are the methods supported by the String class

Java Common class-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.