Study number 2016-2017-2 "Java Program design" Sixth week study summary textbook Learning content Summary Eighth chapter common practical class String class
- Constructs a String object: a constant object; string object; Reference string constant.
- Strings are collocated: The string object uses "+" for the collocated operation, that is, the end-to-end phase.
- Common methods of the string class
public int length()
: Gets the length of the string object.
public boolean equals(String s)
: Compares whether the character sequence of the current string object is the same as the character sequence of the string object specified by the parameter S.
public boolean startsWith(String s)
: Determines whether the prefix of the current string object's character sequence is consistent with the string object specified by the parameter.
public boolean endsWith(String s)
: Determines whether the suffix of the current string object's character sequence is consistent with the string object specified by the parameter.
public int compareTo(String s)
: The size of the character sequence of the string object s specified by the dictionary order with the parameter.
public boolean contains(String s)
: Determines whether the character sequence of the current string object contains the character sequence of the parameter S.
public int indexOf(String s)
: Returns the position of the first occurrence of the character sequence of S, starting at the 0 index position of the character sequence of the current string object. If it cannot be retrieved, it returns-1. Note: The indenxOf(String str,int startpoint)
method is an overloaded method that specifies where to start the search. spaces also occupy the position of a character sequence .
public int lastIndexOf(String s)
: Returns the position of the last occurrence of the character sequence of S, starting at the 0 index position of the character sequence of the current string object. If it cannot be retrieved, it returns-1.
public String substring(int startpoint,int end)
: The calling method obtains a new string object, and the new string object is a sequence of characters that copies the current startpoint position to the end-1 position. You can also omit end and copy all the sequence of characters from StartPoint to the end.
- Conversion of strings to basic data:
public static String valueOf(byte/int/long/float/double n)
converts the above type to a string object.
- The string representation of the object: The object class has a
public String toString()
method that can be used to obtain a string representation of the object by calling the method. The returned form is: A string representation of a reference to the name of the class that created the object @ object.
- string and character array, byte array.
An example of a string and a character array illustrates:
String s="1945年8月15日是抗战胜利日";char []a=new char[4];s.getChars(11,15,a,0);//数组a的单元依次放的字符是抗 战 胜 利
char []c;c="睡觉".toCharArray();//数组c的单元依次放的字符是 睡 觉
- String and byte arrays: The construction method of the string class
String (byte[],int offset,int length)
, starting at offset from the beginning of the array, taking the length byte, and constructing a string object.
- Character encryption algorithm: If the encryption algorithm is to do the addition operation, then the decryption algorithm is the subtraction operation.
- The substitution and decomposition of regular expressions and strings
- A regular expression is a sequence of characters of a string object that contains characters that have special meanings, which are called meta-characters of regular expressions.
- Metacharacters
- Qualifier
- String substitution: The
public String replaceAll(String regex,String replacement)
calling method returns a new string object, and the new string object's character sequence is a sequence of sub-characters that match all of the current object character sequences to the Regex, and the new character sequence is replaced with the character sequence of the parameter replacament, But does not affect the character sequence of the current string object.
Decomposition of a sequence of characters: public String[]split(String regex)
when the method is called by a string object, the regular expression specified by using the parameter regex breaks out the word in the character sequence of the current string object as a delimited tag and stores the decomposed word in a string array, such as:
String str="1949年10月1日建国";String regex="\\D+";//匹配任何非数字字符序列String digitWord[]=str.split(regex);//digitWord[]存放的为:1949 10 1
StringTokenizer class
Problems in teaching materials learning and the solving process
Textbook learning Problems first go to https://shimo.im/doc/1i1gldfsojIFH8Ip/to see, if others did not ask the same question, you can edit the document to add, and then copy their questions to the following:
- Question 1:xxxxxx
- Issue 1 Solution: XXXXXX
- Question 2:xxxxxx
- Issue 2 Solution: XXXXXX
- ...
Problems in code debugging and the resolution process
Code debugging in the textbook have a problem first go to https://shimo.im/doc/1i1gldfsojIFH8Ip/to see, if others did not ask the same question, you can edit the document to add, and then copy their questions to the following:
- Question 1:xxxxxx
- Issue 1 Solution: XXXXXX
- Question 2:xxxxxx
- Issue 2 Solution: XXXXXX
- ...
Code Hosting
Last week's summary of the wrong quiz
- Wrong question 1 and reason, understand the situation
- Wrong question 2 and reason, understand the situation
- ...
Pairing and mutual evaluation reference
20165324 Java Programming Week sixth