Java note 8. Common API callback strings and javaapi callback strings

Source: Internet
Author: User

Java note 8. Common API callback strings and javaapi callback strings
Reprinted string processing class and interface, please indicate the source: http://blog.csdn.net/u012637501 (embedded _ small J of the sky)
I. String and StringBuffer classes1. string typeJava defines the String and StringBuffer classes to encapsulate various operations on the String. They are all put into the java. lang package. (1) String class: Once initialized, the content of a String class object cannot be changed. The String class is used to compare two strings, find and extract the characters or substrings in the String; conversion between strings and other types. Use StringBuffer, StringBuilder, or "+" to concatenate characters. (2) StringBuffer class: The StringBuffer class is used to change the content (string) of an object. It can add, insert, or flip the original characters in other types of data. This class is safe to use in multiple threads and can be synchronized. In addition, once StringBuffer is used to generate the final desired String, you can use the StringBuffer. toString () method to convert it to the String class, and then you can use various methods of the String class to operate the String. (3) StringBuilder class: The StringBuffer class is used to change the content (string) of an object. It can add, insert, or flip other types of data to a string, the StringBuffer class has the same method (the most important method is append and insert ). This class can only be used in a single thread and cannot be used for content synchronization, but its speed is faster than that of StringBuffer. (4) CharSequence interface: CharSequence is only a readable Character Sequence Value. This interface provides unified, read-only access to many different types of character sequences. The CharSequence interface cannot instantiate an object. 2. constructor and member functions
String class:
(1) constructor:. string (): Creates a String object whose content is a NULL Character Sequence. B. string (byte [] bytes): Creates a String object whose content is a byte [] bytes String sequence. c. string (String original): Creates a String object. The content of the object copies the content of the original object. d. string (StringBuffer buffer): Creates a String object whose content is included in the buffer object. e. string (StringBuilder builder): Creates a String object whose content is included in the builder object. (2) common member method a.intCompareTo (String anotherString ):Compares two strings. The parameter is a string object. string concat (String str): adds the String of the str object to the end of the String. c. boolean contains (CharSequence s): determines whether the string contains the string sequence sd. boolean inclusignorecase (String anotherString): determines whether a String object is equal to another character wearing object, regardless of case. e. boolean equals (Object anObject): determines whether the content of the two string objects is equal; f.int indexOf (int ch): returns the number g of the first occurrence of the 'ch' character in the string. indexOf (int ch, int fromIndex): returns the number h of the 'ch' character that appears for the first time from the fromIndex position in the string. boolean isEmpty (): determines whether the string object is null. static St Ring join (CharSequence delimiter, CharSequence... elements): link a string of characters k.int length (): Obtain the string length l. string replace (char oldChar, char newChar): replaces one character in the original String to generate a new String object String replace (CharSequence target, CharSequence replacement) instead of changing the principle of the String content replaceAll (String regex, String replacement) m. string substring (int beginIndex, int endIndex): truncates a substring (from deginIndex to endIndex) n. char [] toCharArray (): converts a string to a character array and saves it to c O. String toString () in the har array: convert other objects to the String object String p. static String valueOf (...) : Convert other types of characters/parameters/to string objects
StringBuffer class
(1) constructor:. stringBuffer (): Creates a StringBuffer object, initializes a 16-character space, and the storage content is empty. B. stringBuffer (CharSequence seq): Creates a StringBuffer object whose initial content is seq; c. stringBuffer (int capacity): Creates a StringBuffer object and specifies the space size as capacity; d. stringBuffer (String str): Creates a StringBuffer object whose initial content is the String specified by the str object. (2) common member method. stringBuffer append (char c/char [] str/CharSequence s /...): add the corresponding content to the end of the string sequence; B. stringBuffer delete (int start, int end): delete the string sequence from start to end. c. stringBuffer insert (int offset, char [] str/CharSequence s /...): Insert a character/string/..d.int length () to the offset position of the string sequence: returns the number of characters in the string sequence specified by the StringBuffer object; e. stringBuffer replace (int start, int end, String str): replace the specified String sequence f. string substring (int start, int end): truncates the substring sequence g. string toString (): converts a StringBuaffer object to a String object;
StringBuilder classPublic final class StringBuilderextends Objectimplements Serializable, CharSequence constringbuilder (): Creates a 16-character StringBuilder object StringBuilder (CharSequence seq) with empty content: Creates a StringBuilder object, the object content is seq String sequence StringBuilder (int capacity): Creates a String Builder object. The object storage space is caoacityStringBuilder (String str): Creates a StringBuilder object, the object content is a string represented by the str object
Ii. Induction of application skills(1) String str = "abc"; is an anonymous String object named str. equivalent to: char data [] = {'A', 'B', 'C'}; String str = new String (data); (2) join operator (+ ): converts other types of data into strings and concatenates them into new strings. For example, String x = "a" + 4 + "c "; it is equivalent to String x = new StringBuffer (). append (""). append (4 ). append ("c "). toString (); (3) String str = new StringBuffer (). toString (); converts a StringBuffer object to a String-type object String (4) byte [] buf = new byte [1024]; String str = new String (buf, 0, pos ); converts a byte array to a String-type object String (5) Xxx packaging class. parseXxx: convert a string to a basic data type (6) Xxx packaging class object. xxxValue: Convert the packaging class to the basic data type (7) String s1 = "hello"; String s2 = "hello "; the two equal signs "=" above indicate that s1 and s2 are the same object String s1 = new String ("hello"); String s2 = new String ("hello "); although the content is the same, there are actually two different objects.
Iii. instance source code

 
Import java. io. IOException;/* learn to use the String class: The program reads non-stop input strings from the keyboard one line by one * and prints the display, know to enter a line "bye" Until */public class ReadLine {public static void main (String [] args) {byte buf [] = new byte [1024]; // at this time, byte is the basic data type String finalInput = null; int pos = 0; int ch = 0; System. out. println ("Enter data:"); while (true) {// 1. read a byte from the keyboard. try {ch = System. in. read (); // returns a byte read from the keyboard, which may throw an IOException exception} catch (IOException e) {e. printStackTrace ( );} // 2. process the data read from the keyboard switch (ch) {case '\ R': break; case' \ N': // enter the Enter key: Buffer buf space 0 ~ Data in the pos range is converted to a String and finalInput = new String (buf, 0, pos) is determined; if (finalInput. equalsIgnoreCase ("bye") {return; // exit program} else {System. out. println (finalInput); pos = 0; break;} default: // if you do not press the Enter key, continue to put the entered characters in the buf space buf [pos ++] = (byte) ch; break ;}}}}


Reference: http://docs.oracle.com/javase/8/docs/api/index.html

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.