Description of use of the string class

Source: Internet
Author: User

1 The number of characters (length) contained in the string

int length ();

Note: Arrays also have length, which is length, but the lengths of the arrays are attributes, without parentheses.

1.2 Gets the position of a character based on its position.

char charAt (int index) occurs when you access a script that does not exist in a string, a string header out-of-bounds exception will occur: Stringindexoutofboundsexception.

1.3 Gets the position of the character in the string, based on the character.

int indexOf (int ch); The incoming parameter is the ASCII code, and the first occurrence of CH in the string is returned.

int indexOf (int ch,int fromIndex): Gets the position of ch in the string, starting at the FromIndex specified position.

No access (this character is not in the string) returns-1.

1.4 Gets the string's position in the string based on the string (small string is found in a large string)

int indexOf (string str); The incoming parameter is ASCII and returns the position of the first occurrence of STR in the string.

int indexOf (String str,int fromIndex): Gets the position of STR in the string, starting at the FromIndex specified position.

1.5 Gets the position of the character in the string, based on the inverse of the character

int lastIndexOf (int ch); The incoming parameter is the ASCII code, and the first occurrence of CH in the string is returned.

int lastIndexOf (int ch,int fromIndex): Gets the position of ch in the string, starting at the FromIndex specified position.

1.6 Gets the position of the string in the string backwards based on the string

int lastIndexOf (string str); The incoming parameter is ASCII and returns the position of the first occurrence of STR in the string.

int lastIndexOf (String str,int fromIndex): Gets the position of STR in the string, starting at the FromIndex specified position.

2. Judging

2.1 Whether a substring is contained in a string.

Boolean contains (String str);

Special variant: IndexOf (String str) can be the position where Str first appears, if 1 is returned, indicating that STR does not exist in the string. Therefore, it can also be used to determine whether the specified string is included. Moreover, the method can be used to judge and obtain the position of occurrence.

Application: If only for judging, with contains, if you have to judge, but also get position with indexof

2.2 Whether there is content in the string

Boolean isEmpty (); The principle is to determine whether the length is 0

2.3 Whether the string starts with the specified content

Boolean startsWith (String str)

2.4 Whether the string ends with the specified content

Boolean endsWith (String str)

2.5 Determine if the contents of a string are the same

Boolean equals (String str) has replicated the Equals method in the object class.

2.6 Determine if string contents are the same and ignore case

Boolean equalsignorecase ()

3. Conversion

3.1 Converting a character array into a string

Constructor: In the constructor of a string, there is a constructor that is passed in the character array.

String (char[] value)

When the string is initialized, it is possible to complete the operation of converting the character array into a string.

And one of its overloaded constructors

String (char[] Value,int offset,int count): Converts a portion of a character array to a string

The second parameter is offset, and the third parameter is the number

Similarly: There is also the same constructor for a byte array. Just one more set of encodings

Static functions are static, String copyvalueof (char[] data), and note that it is statically, because it does not operate unique data internally. He also has overloaded functions that convert part of a character array into a string

Static String copyvalueof (char[] data,int offset,int count)

Static String valueOf (char[] data)

3.2 Converting a string into a character array * *

Find API Tips: Certainly not constructs, the number of groups, there will be a return value, and is a char type array so according to the method of return value type to find

Char[] ToCharArray (), why not pass in a string parameter here? Because the method is called by an object, the object that calls the method here is a string, so there is a hidden this inside the parentheses.

3.3 Converting a byte array to a string (IO)

String (byte[] value)

String (byte[] Value,int offset,int count) converts a portion of a byte array to a string

3.4 Converting a string into a byte array

Byte[] GetBytes ()

3.5 Convert basic data to a string.

static String valueOf (int)

Static String valueOf (double)

Example: string.valueof (3) = = "3" = = "" "

Special: Strings and byte arrays can be specified in the encoding table during the conversion process.

4. Replace

String replace (char Oldchar,char Newchar), it is important to note that a new string is returned because the string cannot be modified once it has been created, so it returns a new string.

If the character to be replaced does not exist, the original string is returned. But such a character substitution of one character is cumbersome. Java gives us another way.

String replace (charsequence target,charsequence Replacement) This method can receive and replace strings directly. The previous parameter is the old string, and the latter argument is the new string.

There are two other ways of doing this:

String ReplaceAll (string regex,string replacement);

String Replacefirst (string regex,string replacement);

Because a regular expression is involved, it is not studied for the time being. Understand that it is replaced by the specified rules.

5. Cutting

Note: A string is separated into several strings, and a string array is returned.

String[] Split (String regex);

The parameters passed in here are also a rule, but the rules are a bit special, and you don't have to specify a rule (regular expression) and write a generic string.

String s = "Zhangsan,lisi,wangwu";

string[] arr = S.split (",");

for (Intx = 0;x<arr.length;x++) {

System.out.println (Arr[x]);

}

Note: The split is not equal to get, in this case there is "," in the string, and the resulting array is not "," and the standard string of the split string is not obtained.

6. Sub-string. Gets a portion of the string (which can be placed in the fetch)

String substring (int beginindex): from the specified position to the end.

String substring (int beginindex,int endIndex) contains a header and does not contain a tail.

If the foot tag does not exist, a string pin out-of-bounds exception occurs.

In the case of cutting, we can index the comma's foot mark, and get the name in the method here. Is trouble, no cutting fast.

7. Conversion

Converts a string to uppercase or lowercase.

String toUpperCase ();

String toLowerCase ();

8. Remove spaces

Remove multiple spaces at both ends of a string

String trim ();

9 comparison

A comparison of the natural Order (dictionary order) of two strings

int CompareTo (String str)

There are three kinds of results: greater than, less than, equal to

Object equals the passed in parameter returns 0

object is less than the passed-in parameter returns a negative number

object is greater than the parameter passed in returns a positive number

The returned value is the result of the ASCII code value of the same pin corresponding to the same character in the two string, if the same will always be compared to the next, until the difference, if different, the output, no longer continue than.

10

JDK1.5, the String class adds a useful static method String.Format (): Format (Locale L, String format, Object ... args) using the specified locale, Format strings and parameters return a formatted string. Format (string format, Object ... args) returns a formatted string using the specified format string and parameters.

Description of use of the string class

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.