What's the difference between int and integer
Difference 1: Different types
Integer is the wrapper class for int;
int is the basic data type;
Difference 2: Different instantiation references
Integer variables must be instantiated before they can be used;
The int variable is not required;
Difference 3: Storage locations are different
Integer is a reference to an object that points to this new Integer object;
int is the direct storage of data values
Difference 4: Storage defaults are different
The default value for Integer is null; The default value of int is 0.
The conversion of int and integer into boxes and unboxes;
Packing:
Is the process of converting a basic data type to a wrapper class.
For example: int to Integer
int i=333;
Integer I=Integer.valueOf(i);
Packing is also divided into manual and automatic packing
Manual boxing requires calling the wrapper class valueOf() method, which returns the object.
Automatic packing is correct. Hidden call of valueOf(), automatic conversion of Java virtual machine;
For example:
Integer I=10;
Character Ch='b';
Unpacking:
Is the process of converting a wrapper class into a basic data type;
For example, convert Integer to int type
Integer In=10;
int a=In.intValue();
Unpacking is divided into automatic and manual
booleanValue() boolean to boolean
bytevalue() Byte to byte
Short Value() Short to short
intValue() Integer to int
longValue() Long to long
floatValue() Float to float
doubleValue() Double to double
charValue() Character to char
Manual unboxing: This is the call unboxing method shown
For example:
Boolean a=new Boolean(false);
boolean a1=a.booleanValue();
Automatic unboxing: implicitly invoke the unboxing method
For example:
Integer j=10;
int i=j++;
Regular Expression Detailed Cutting
Regular expressions are a specification for pattern matching and substitution.
For example, regular expressions:
^[1-9]\d{5}$denotes a QQ number
A regular expression is a text pattern composed of characters and special characters.
Functions:
Find, extract, split, replace strings, etc.
Any string defined in java can be used as a regular expression
How to determine whether a string matches a regular expression
- Mode 1
1 Compile a string into a Pattern Object.
Pattern p = Pattern.compile(regular expression);
2 Use Pattern Object to Create Matcher Object
Matcher m = p.matcher(String to match);
3 Call Matcher class matches() method
m.matches() Return true Represents a match, returns false Indicates a mismatch.
- Mode 2
Use the static method matches() under Pattern s.
Pattern.matches(Regular, string)
Sample Code
String regex = "abc"; String str = "abc"; System.out.println(Pattern.matches(regex, str));
- Mode 3
A direct match method is provided under the String class.
str.matches(regular)
Common symbols for regular expressions
- predefined character classes
. | Any character (may or may not match line terminators) |
---|---|
\d | Number: [0-9] |
\D | Non-numeric: [^0-9] |
\s | Blank characters: [\t\n\x0Bf\r] |
\S | Non-whitespace character: [^\s] |
\w | Word character: [a-zA-Z_0-9] |
\W | Non-word character: [^\w] |
- Greedy Quantifier
X? | X, neither once nor once |
---|---|
X* | X, zero or more times |
X+ | X, one or more times |
X{n} | X, exactly n times |
X{n,} | X, at least n times |
X{n,m} | X, at least n times, but no more than m times |
Regular Matching Function
Requirements: Verification QQ Number, Requirement: Must be 5~15 Number of digits, 0 cannot start.
- Before regular expression
public static void checkQQ(String qq){ int len = qq.length(); if(len>=5 && len <=15){ if(!qq.startsWith("0")){ try{ long l = Long.parseLong(qq); System.out.println("qq:"+l); } catch (NumberFormatException e){ System.out.println("Illegal Character Occurred"); } } else System.out.println("Do not start with 0"); } else System.out.println("QQ Number length error"); }
- Use regular expressions
public static void checkQQ2(){ String qq = "12345"; String reg = "[1-9][0-9]{4,14}"; boolean b = qq.matches(reg); System.out.println("b="+b); }
Regular Cutting Function
Requirement 1: Cut a string based on spaces.
public static void testSplit(){ String str = "-1 99 4 23"; String[] arr = str.split(" +"); for(String s : arr){ System.out.println(s); } }
Requirement 2: Cut based on overlapping words
public static void testSplit2(){ String str = "adqqfgkkkhjppppcat"; String[] arr = str.split("(.)\\1+"); for(String s : arr){ System.out.println(s); } }
- Range representation
[abc] | a, b, or c (simple class) |
---|---|
[^abc] | Any character except a, b or c (negative) |
[a-zA-Z] | A to Z or A to Z, both letters included (range) |
[a-d[m-p]] | A to D or m to p:[a-d m-p] (union) |
[a-z&&[def]] | d, e, or f (intersection) |
[a-z&&[^bc]] | a to z, except b and c:[ad-z] (minus) |
[a-z&&[^m-p]] | a to z, not m to p |
Word Boundary\b
\b denotes the word boundary, and all characters except those in \w can be used as the word boundary.
For example, "hi. *" matches "high"
"ahi.*" and "ahigh" matching "a.hi.*"and "axhigh" matching but "a.\\bhi.*" and "axhigh" Why does it not match?
Because **\b** indicates that the bounding space of a word divides a and high into two words, that is, high is the beginning of another word on the boundary, so it matches. x does not split a and high into two words.
Grouping Regular Expressions
Use grouping to resolve duplicate multiple characters;
Use Grouping: In regular expressions, use () to represent grouping
Grouping is the use of parentheses to enclose items into separate logical domains, so that the contents of parentheses can be treated as if they were a separate unit.
This grouping is called a capture group, or a reverse reference.
Capture group refers to the use of **\group number ** to indicate which group of contents precedes the reference.
For example:
String regex = "((a)(b©))";
//How to determine grouping
//View left brackets
//Group 1: ((a)(b) ©))
//Group 2: (a)
//Group 3: (b) ©)
//Group 4: ©
Regular Matching Function
Requirements: Verification QQ Number, Requirement: Must be 5~15 Number of digits, 0 cannot start.
- Before regular expression
public static void checkQQ(String qq){ int len = qq.length(); if(len>=5 && len <=15){ if(!qq.startsWith("0")){ try{ long l = Long.parseLong(qq); System.out.println("qq:"+l); } catch (NumberFormatException e){ System.out.println("Illegal Character Occurred"); } } else System.out.println("Do not start with 0"); } else System.out.println("QQ Number length error"); }
- Use regular expressions
public static void checkQQ2(){ String qq = "12345"; String reg = "[1-9][0-9]{4,14}"; boolean b = qq.matches(reg); System.out.println("b="+b); }
Regular cutting function
Requirement 1: Cut a string based on spaces.
public static void testSplit(){ String str = "-1 99 4 23"; String[] arr = str.split(" +"); for(String s : arr){ System.out.println(s); } }
Requirement 2: Cut based on overlapping words
public static void testSplit2(){ String str = "adqqfgkkkhjppppcat"; String[] arr = str.split("(.)\\1+"); for(String s : arr){ System.out.println(s); } }
Regular Replacement Function
String Class replaceAll Method
Sample Code
public class Demo13_Replacement function { public static void main(String[] args) { String str = "www.baidu.com"; String str2 = str.replace("w", "x"); System.out.println(str2);//xww.baidu.com String str3 = str.replaceAll("w","x"); System.out.println(str3);//xxx.baidu.com String str4 = str.replaceAll(".", "x"); System.out.println(str4);//wwwxbaiduxcom String str5 = str.replaceAll("[a-z]", "x"); System.out.println(str5);//xxx.xxxxx.xxx } }
Requirement 1: Replace phone number with ****** String str = "Contact me: 18610637606 Contact me: 13510637606 Contact me: 13810637606 Contact me: 18610637606 Contact me: 17610637606 Contact me: 18910637606";
Sample Code
String str = "Contact me: 18610637606 Contact me: 13510637606 Contact me: 13810637606 Contact me: 18610637606 Contact me: 17610637606 Contact me: 18910637606"; String reg = "(\\d{3})(\\d{4})(\\d{4})"; String x = str.replaceAll(reg, "$1****$3"); System.out.println(x);
Requirement 2: String str = "I, I, I, I, I, I want to want to want to want to want to want Nunununununununu to work hard"; // Restore to "I want to work hard"
Sample Code
public static void main(String[] args) { String str = "I want to have holidays and vacations"; // "I'm going on vacation" String regex = "(.)\\1+"; str = str.replaceAll(regex, "$1"); // $1 takes content from a group outside a regular expression System.out.println(str); }
1.2 Regular Get Function (Find Substring)
Finding substrings is also done by Pattern s and Matcher.
Common methods under Matcher:
- Whether find() contains a matching regular substring
- group() takes out the substring
Requirement: Find all four length substrings in a string
Sample Code
public class Demo1{ public static void main(String[] args) { String str = "aaa abcd axss xssdf"; // Find all 4 length substrings String regex = "\\b[a-zA-Z]{4}\\b"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); while(m.find()){ System.out.println(m.group()); } } }
Regular Conversion
contains API source
public boolean contains(Object o) { Iterator<E> it = iterator(); if (o==null) { while (it.hasNext()) if (it.next()==null) return true; } else { while (it.hasNext()) if (o.equals(it.next())) return true; } return false; }
split method
Split a string into substrings and return the result as an array of strings
substring() method of string class
Method Introduction:
public String substring(int beginIndex)
beginIndex - Start index (inclusive).
Returns a new string, which is a substring of this string. The substring begins with the character at the specified index and ends with the string.
Example:
"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)
Second
public String substring(int beginIndex,
int endIndex)
Example:
"hamburger".substring(4, 8) returns "urge"
smiles".substring(1, 5) returns "mile"
Returns a new string, which is a substring of this string. The substring starts at the specified beginIndex and continues until the character at index endIndex - 1. Therefore, the length of the substring is endIndex-beginIndex.
Example: