APIs and regular expressions

Source: Internet
Author: User

Chapter I. String & StringBuilder
The string class is decorated with the class final and cannot be inherited, and the string string can never be changed after it is created, but a reference to it may be re-assigned, changing the reference point to
Any character in the Java string corresponds to a fixed-length Unicode encoding of 16 bits (2 bytes);

Java to improve performance, static strings (the result of literals, constants, Constant joins) are created in a constant pool and use the same object as much as possible, reusing static strings
For recurring string literals, the JVM first looks in the constant pool and returns the object if it exists;
*str.length (); Gets the number of characters in the string;
*int str.indexof (String str)//detects the first occurrence of STR in a string, returns an int type subscript value, and cannot find the return-1 value
*int indexOf (String str,int fromIndex)//Retrieves the first occurrence of Str from the FromIndex position of the string, and returns the subscript
*int lastIndexOf (String str,int from)//str the last occurrence in the string
Interview questions:
String str = "java, I love Java, I love Java, I love Java";
Q: Number of occurrences of substring Java

int index=0; // The subscript that appears in Java int count=0; // number of occurrences of Java  while (Index=str.indexof ("Java", "index")!=-1) {System.out.println ("index=" +Index), Count+ +,index + = "java". Length ();} System.out.println (count);

*string substring (int beginindex)//intercept substring starting from the specified subscript to the end of the end of the string, the return value is of type string
*string substring (int beginindex,int endIndex);//intercept the substring starting from the specified subscript to the end of the specified subscript, and the return value is string type
(many methods in Java often have two critical values, usually the header does not wrap the tail)
Cases
Truncate the string between the first comma and the last comma

int beginindex=str.indexof (","); int endindex=str.lastindexof (","= str.substring (beginindex+1, EndIndex); System.out.println (sub);


Note: In Java string: The Chinese Input Method "," and the English IME "," is different, write a string can result in the query result is 1 (that is, the query is not)

*str.trim ();//Remove whitespace character format at the beginning and end of the string str

*char charAt (int index);//returns the char character at the specified subscript

*str.startswith (string prefix);//Determines whether the STR string starts with the specified prefix prefix, and returns the Boolean judgment result

*str.endswith (string suffix);//Determines whether the STR string ends with the specified suffix suffix, and returns the Boolean judgment result


StringBuilder string Generator
StringBuilder sub = new StringBuilder ();//initial 16 characters can be placed
*. append (string str); For the implementation of a large number of string concatenation
*. insert (int offset,string str), inserts the specified content at the specified location
*. delete (int start,int end); Removes the string from the specified position to the end of the specified position
*. reverse (string str), completion of string reversal

-------------------------------------------------------------------------------
Chapter II regex Regular Expressions & Object class & wrapper class

RegularExpression Regular Expressions
Definition: A string of "rule strings" consisting of specific characters used to describe/record text rules.
Meaning: It is convenient to implement complex operations such as matching, finding, replacing strings.

1) Character Set:
[ABC]: Any one of the A,b,c
[^ABC]: any character except A,b,c
[A-z]: Any character from A to Z
[a-za-z0-9]: A to ZA to z0~9 any one character
[A-z&&[^bc]]:a to Z any character other than B and C,&& the relation of the expression;

2) Pre-defined character set
. : any one character
\d: Any numeric character equivalent to [0-9];
\d: Non-numeric characters, equivalent to [^0-9];
\w: A word character, equivalent to [a-za-z_0-9];
\w: non-word characters
\s: whitespace characters
\s: non-whitespace characters

3) Quantity Words
X? : represents 0 or 1 of
X*: represents 0 or any number of
X+: represents 1 to any number of
X{n}: Represents n x
X{n,m}: Represents n to M x

4) grouping (), a series of regular expressions as a whole, you can use the | representation or the relationship
\ character Escapes
^ $: bounds match, ^ change is string start, $ indicates end of string

String Regular API

1) str.matches (regex):
Matches a string to a regular expression, and the match returns true, otherwise false
* The user enters a string to validate the string
* Must satisfy the first character is any one character of ABC
* The second character is an arbitrary number

Scanner sc=New  Scanner (system.in); Boolean Flag;  Do {System.out.println ("Please enter user name, start with A/B/C, include number"=sc.next (); String regex= "[abc][0-9]{2,8}"= str.matches (regex); if (flag==false) {System.out.println ("input error, please reenter:")} Else {System.out.println ("correct:");}}  while (flag==false);

Str.matches (regex) default comes with ^$, must be string.matches (regex) when matching

Determine if the email format is legitimate:
String regex1 = "^[a-za-z0-9_.-][email protected" ([a-za-z0-9]+\\.) +[a-za-z]{2,4}$ ";

2) Str.split (string regex); return value is a string array
String [] s = email.split ("[[email protected]\\.]");
System.out.println (arrays.tostring (s));
[Mr, W123, Baidu, COM, CN]

3) ReplaceAll (String regex,string replacement)
Replace a string in a string that matches the regular expression regex rule with a replacement
String s2= "China, no country among the nations";
String s3=s2.replaceall ("China", "*");
System.out.println (S3);
*, no country in the country

Object (Ancestor Class)
If you define a Java class without declaring its parent class with the extends keyword, its parent class defaults to: Java.lang.Object class
Reference variables of type object can point to any type of objects.

1) ToString () Overrides
* ToString () in the object class returns the string representation of an object by default,
* and this string does not have any specific meaning in the actual beginning, it is generally strongly recommended that
* override ToString () in subclasses Make it meaningful
* Rewrite rules: Typically this method returns the property value of an object as a string
/*
* in memory, point is a reference, the address value of the saved object
* When the output reference is printed, the class's ToString () is called, if the class does not have
* Then, call ToString ()
in the parent class () in the object class: Returns a String representing the object
* String form: Class name @ hash code (16 integers)
* A hash code is an integer computed by an algorithm based on the address value
*/
Point point = new Point (ten);
System.out.println (point); The coordinates of the point represented by this object

2) the Equals () override
* Equals () method in the object class is equivalent to = =
* For comparing two references to the same object
* in real development, It is generally recommended to override the Equals method based on the actual business, making comparisons between
* objects more practical. Many classes in the
* Javaapi have overridden equals (), such as String

Packing class
In the context of type conversion, there is a special conversion that requires the conversion of a basic type such as int to an object;
All basic types have a corresponding class, the wrapper class (wrapper)
A wrapper class is an immutable class that does not allow changes to the value that is wrapped in a wrapper class object after it has been constructed
The wrapper class is final and cannot define their subclasses.
Number abstract class, which is the parent class of the Byte,double,float,integer,long,short class;
The subclass of number must provide a method (convert the represented value to a base type such as Byte,double,float,int,long,short)
Intvalue () returns the specified value in int form
Doublevalue () returns the specified value in double form
Floatvalue () returns the specified value in float form
Number n = 123;
Number m=123.45;
int a =n.intvalue ();//a=123;
Double d =n.doublevalue ();//d=123.0

int A1 =m.intvalue ();//a1=123;
Double D1 =m.doublevalue ();//d1=123.45;


1) Integer common functions:
Represents a constant:
The static int max_value value is 2, 31 times minus 1, and the maximum value that the int type can represent
The static int min_value value is 31 squares of 2, indicating the minimum value that the int type can represent
2) Integer static method parseint: Used to pass a string to int

Integer i =new integer (3);//convert 3 to Object I
Double D=i.doublevalue ();//The value in the object is used for the calculation,
System.out.println (d);//3.0
Double D1 =double.valueof (d);//The value represented by the number is used for object-oriented development

/*
* parseint (string), converting string to int type
* The precondition of the conversion is that the value of the string type must be an integer type
* Otherwise fails, throws an exception
*/

String str = "123"; int s1=integer.parseint (str); System.out.println (S1+4); // 123+4=127str= "A123"; // s1=integer.parseint (str); System.out.println (S1); // numberformatexception number Format exception

APIs and regular expressions

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.