Detailed parsing of string classes in Java

Source: Internet
Author: User

Strings are widely used in Java programming, strings belong to objects in Java, Java provides string classes to create and manipulate strings, and this article details the contents of the string class.

Create a string

The simplest way to create a string is as follows:

String greeting = "php Chinese web";

When you encounter a string constant in your code, the value here is "php Chinese ", which the compiler uses 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:

public class stringdemo{public static void Main (String args[]) {  char[] Helloarray = {' P ', ' h ', ' P '};   String hellostring = new string (helloarray);   System.out.println (hellostring);    }    }

The results of the above example compilation run as follows:

Php

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

If you need to make a lot of changes to the string, you should choose to use the StringBuffer & StringBuilder class.

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 14:

Stringdemo.java File Code:

public class Stringdemo {public static void main (String args[])  {   string site = "www.php.cn";   int len = Site.length ();   System.out.println ("php Chinese web URL length:" + len);}   }

The results of the above example compilation run as follows:

PHP Chinese web site: 14

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 ("PHP");

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

"Hello," + "PHP" + "!"

The results are as follows:

"Hello, runoob!."

Here is an example:

Stringdemo.java File Code:

public class Stringdemo {public static void main (String args[])  {   string string1 = "PHP Chinese web site:";    System.out.println ("1," + string1 + "www.php.cn");    } }

The results of the above example compilation run as follows:

1, PHP Chinese Web site: www.php.cn

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 write that too.

String FS; FS = 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)

String method

The following are the methods supported by the string class, in more detail, see the Java string API documentation:

SN (serial number) Method Description
1 char charAt (int index)
Returns the char value at the specified index.
2 int CompareTo (Object o)
Compare this string to another object.
3 int CompareTo (String anotherstring)
Compares two strings in a dictionary order.
4 int comparetoignorecase (String str)
Compares two strings in a dictionary order, regardless of case.
5 String concat (String str)
Connects the specified string to the end of this string.
6 Boolean contentequals (StringBuffer SB)
Returns True when and only if the string has the same order of characters as the specified stringbuffer.
7 Static String copyvalueof (char[] data)
Returns a String representing the sequence of characters in the specified array.
8 Static String copyvalueof (char[] data, int offset, int count)
Returns a String representing the sequence of characters in the specified array.
9 Boolean endsWith (String suffix)
Tests whether this string ends with the specified suffix.
10 Boolean equals (Object anobject)
Compares this string to the specified object.
11 Boolean equalsignorecase (String anotherstring)
Compares this string to another string, regardless of case.
12 Byte[] GetBytes ()
This String is encoded as a byte sequence using the platform's default character set, and the result is stored in a new byte array.
13 Byte[] GetBytes (String charsetname)
Encodes this String into a byte sequence using the specified character set and stores the result in a new byte array.
14 void GetChars (int srcbegin, int srcend, char[] DST, int dstbegin)
Copies the character from this string to the target character array.
15 int Hashcode ()
Returns the hash code for this string.
16 int indexOf (int ch)
Returns the index at which the specified character first appears in this string.
17 int indexOf (int ch, int fromIndex)
Returns the index at the first occurrence of the specified character in this string, starting the search from the specified index.
18 int indexOf (String str)
Returns the index at which the specified substring first appears in this string.
19 int indexOf (String str, int fromIndex)
Returns the index of the first occurrence of the specified substring in this string, starting at the specified index.
20 String Intern ()
Returns a normalized representation of a string object.
21st int lastIndexOf (int ch)
Returns the index of the specified character at the last occurrence in this string.
22 int lastIndexOf (int ch, int fromIndex)
Returns the index of the last occurrence of the specified character in this string, starting at the specified index to reverse-search.
23 int lastIndexOf (String str)
Returns the index of the rightmost occurrence of the specified substring in this string.
24 int lastIndexOf (String str, int fromIndex)
Returns the index of the last occurrence of the specified substring in this string, starting at the specified index to reverse the search.
25 int Length ()
Returns the length of this string.
26 Boolean matches (String regex)
Tells whether this string matches a given regular expression.
27 Boolean Regionmatches (boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests whether two string regions are equal.
28 Boolean regionmatches (int toffset, String other, int ooffset, int len)
Tests whether two string regions are equal.
29 String replace (char OldChar, char Newchar)
Returns a new string that is obtained by replacing all OldChar that appear in this string with Newchar.
30 String ReplaceAll (string regex, string replacement)
Replaces this string with the given replacement for all substrings that match the given regular expression.
31 String Replacefirst (string regex, string replacement)
Replaces this string with the given replacement to match the first substring of a given regular expression.
32 String[] Split (String regex)
Splits this string according to the match of the given regular expression.
33 String[] Split (String regex, int limit)
Splits the string by matching the given regular expression.
34 Boolean startsWith (String prefix)
Tests whether this string starts with the specified prefix.
35 Boolean startsWith (String prefix, int toffset)
Tests whether the substring starting at the specified index begins with the specified prefix.
36 Charsequence subsequence (int beginindex, int endIndex)
Returns a new sequence of characters that is a subsequence of this sequence.
37 String substring (int beginindex)
Returns a new string that is a substring of this string.
38 String substring (int beginindex, int endIndex)
Returns a new string that is a substring of this string.
39 Char[] ToCharArray ()
Converts this string to a new character array.
40 String toLowerCase ()
Use the default locale rule to convert all characters in this String to lowercase.
41 String tolowercase (locale locale)
Converts all characters in this String to lowercase using the rules for a given Locale.
42 String toString ()
Returns the object itself (it is already a string!) )。
43 String toUpperCase ()
Converts all characters in this String to uppercase using the rules of the default locale.
44 String touppercase (locale locale)
Converts all characters in this String to uppercase using the rules for a given Locale.
45 String Trim ()
Returns a copy of the string, ignoring leading and trailing blanks.
46 Static String valueOf (primitive data type X)

Returns a string representation of the x parameter for the given data type type.

Related Article

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.

Tags Index: