CoreJava Learning 1 -- String processing (String and StringBuilder) & Regular Expression

Source: Internet
Author: User

Here, I have sorted out the previous Java Study Notes and published them into multiple series, which will be released in the future.

This article is divided:

1. Object

2. String and StringBuilder

3. Regular Expression Processing in Java

4. Commons-lang

--------------------------------------------------------------

I. Object

In the Java inheritance structure, at the top is the default inheritance of the quilt class. Everything is an object!

1. toString Method

String of the returned object. By default, the returned format is "Class Name @ hashCode value", which is the handle of the object. However, this method is generally rewritten as needed to return more meaningful information.

2. equals Method

Used to define the equality logic of objects in comparison rules ). Public boolean equals (Object obj), usually used for comparison of reference types. The purpose of rewriting is to compare two objects that are "not like ".

Prototype:

Public boolean equals (Oject obj) {return this = obj}

In the comparison logic, before comparing the content, you must check whether the two objects are of the same type.

Rewriting is based on the actual requirements for defining classes. It does not mean that all attributes must be equal during the comparison.


Ii. String and StringBuilder

1. String

The character series encapsulated by java uses unicode encoding to store strings in the memory. Any character occupies 2 characters.

Feature: A string is a constant object, and the object will not change once it is created.

Is a reference type, but java is easy to use, so you can assign a literal value to the reference type variable ).

String feature constant pool): In terms of performance, java will cache all the strings that appear in the constant pool.

Constant pool: A memory space that is used by JVM to manage strings. In this way, the objective is to reuse strings as much as possible.

Note:/*** when java compiles the source program, it will be optimized when it finds that the content of the Operation twice is literal,

* The computation result is calculated directly during the compilation process, so that the performance is not reduced during each running.

* String str5 = "wor" + "ld"; // source code (. java)

* String str5 = "world"; // The format of the bytecode (. class)

*/


String overrides the equals () of the Object. String also provides the condition signorecase. Another comparison method is used for case-insensitive comparison.

Equals writing habits: literal. equals (variable ). Because if the literal value is written in front, even if the variable is null, no NULL pointer exception occurs. Otherwise, an exception may occur.

Common Methods for String:

1) String toLowerCase (): returns the String in lowercase.

2) String toUpperCase (): returns the String in uppercase.

3) String trim (): The returned String is the original String, removing leading and trailing spaces \ t \ n \ r ...).

4) boolean startsWith (String prefix): determines whether the String starts with a parameter String.

5) boolean endsWith (String suffix): determines whether the String ends with a parameter String.

6) int length (): returns the length of the string.

7) indexOf Method

It is used to retrieve another string in the string and provides the related overload methods:

Int indexOf (String str): searches for str in the String and returns the first occurrence. If not,-1 is returned.

Int indexOf (String str, int fromIndex): similar to the previous method, but it is retrieved from the position of the String fromIndex.

Int lastIndexOf (String str, int from): similar to indexOf, but returns the last position when the str String appears multiple times.

8) charAt Method

Char charAt (int index): returns the character at the specified position of the string. The index parameter indicates the specified position.

Here we can use this method to determine whether a string is a text return, for example:

String str = "Shanxi luoyantan luoxi ";

For (int I = 0; I <str. length ()/2; I ++ ){

If (str. charAt (I )! = Str. charAt (str. length ()-1-i )){

System. out. println ("not a text return ");

Return;

}

}

9) substring Method

Returns the string.

String substring (int beginIndex, int endIndex): returns the substring from the subscript beginIndex (inclusive) to the end of endIndex (excluded.

String substring (int beginIndex): returns the substring from the subscript beginIndex (inclusive) to the end.

10) getBytes Method

Obtains the binary sequence of a string.

Byte [] getBytes (String charsetName): Get according to the specified Encoding

Byte [] getBytes (): It is obtained according to the default system encoding.


Supplement:

Common Datasets

GBK: Country Code

GB2312: supports Chinese standard codes in traditional Chinese characters.

ISO8859-1: European standards

UTF-8: universal international collections


Supplement:

Regular Expression

Description: a regular expression is a string of specific characters that form a "rule string". This rule string is a tool used to describe text rules. A regular expression is the code used to record text rules.

For example, the regular expression "[a-z]" indicates any Russian character from a to z, the regular expression "[a-z] +" indicates a string consisting of one or more strings.

Since the regular expression is a string combination rule, we can combine the expected regular expression according to the rule.


[]: A character can appear.

[Abc] The character can be a, B, or c.

[^ Abc] cannot be a, B, or c.

[A-z] This character can only be lowercase letters

[A-zA-Z0-9] can be letters or numbers

[A-z & [^ bc] is a lowercase letter, but cannot be B or c

.: Any character

\ D: any number, equivalent to [0-9]

\ D: any non-numeric character,

Equivalent to [^ 0-9]

\ S: any blank character

\ S: Non-blank characters

\ W: word character [0-9a-zA-Z _]

\ W: Non-word character [^ \ w]


Quantifiers:

? : 0-1 times

[\ W]? : 0 or 1 word character

[\ W] [\ w]? : 1 or 2 word characters


*: 0-infinity

[\ D] *: Any number

[\ D] [\ d] *: at least once


+: 1-infinity

[\ D] +: at least one number


{N}: n times. n can only be numbers

[\ D] {11}: 11 digits


{N ,}: n or more times

{N, m}: n to m times

[\ D] {3, 5}: 3-5 digits


(): Content as a whole

(Abcd): an abcd will appear.

(Abcd) +: at least one abcd

. Choose one of the following items:

(138 | 135 | 130): 138, 135, or 130


The following lists common Regular Expressions:

[Abc] any character in a, B, and c

[^ Abc] any character except a, B, and c

[A-z] ~ Any character of z

[A-zA-Z0-9] ~ Z, ~ Z, 0 ~ Any character in 9

[A-z & [^ bc] ~ Any character except B and c in z, where & represents the relationship between "and.


. Any character

\ D any number character, equivalent to [0-9]

\ D any non-numeric character, equivalent to [^ 0-9]

\ S blank character, equivalent to [\ t \ n \ x0B \ f \ r]

\ S is not a blank character, [^ \ s]

\ W any word character, [a-zA-Z_0-9]

\ W any non-word character, [^ \ w]


X? 0 or 1 X

X * Indicates 0 or any number of X

X + indicates that 0 to any number of X is greater than or equal to 1 X)

X [n] indicates n X

X [n,] indicates n to any number of X greater than or equal to n X)

X [n, m] indicates n to m X greater than or equal to n and less than m X)


10) split method

Splits a string into a String Array Based on a specific delimiter.

String [] split (String regex): The regex parameter is a regular expression. The String represented by regex is a delimiter and is split into a String array.


11) replace Method

String replacement.

String replaceAll (String regex, String replacement): replace the String matching the regular expression regex in the String with the replacement.


2. Commons-lang

Commons-lang is a component in the Apache Commons project. It provides some useful supplements for the classes in the JDK official java. lang Package and is widely used.

StringUtils is provided in the commons-lang package. For the tool class for string operations, this class provides a series of static practical methods:

1) String repeat (String str, int repeat): repeat the String several times.

2) String join (Object [] array, String separator): concatenates elements in an array into strings.

3) String leftPad (String str, int size, char padChar): Fill the specified character on the left to reach the specified length.

4) String rightPad (String str, int size, char padChar): Fill in the specified character to the Right to reach the specified length.

There are many practical methods. You can view related APIs when using them.


3. StringBuilder

The biggest difference from String is that it encapsulates variable strings, while String is immutable. After an object is created, you can call a method to change the string sequence.

It has the following constructor:

Public StringBuilder ();

Public StringBuilder (String str );

StringBuilder provides append, insert, delete, replace, and other methods to append, insert, delete, and replace character sequences.


Many StringBuilder Methods return StringBuilder and return this.

After the change, the reference of the object is returned.


The difference between String and StringBuffer StringBuilder.

The length of a String is an unchangeable String.

The length of StringBuffer is a variable String. If you operate on the content of the String frequently, especially when the content is to be modified, use StringBuffer. If the String is needed at last, use the toString () of StringBuffer () method. It is thread-safe.

StringBuilder is an equivalent class used by a single thread for the StringBuffer class starting from JDK 5. The StringBuilder class should be used first because it supports all the same operations, but because it does not execute synchronization, It is faster.


This article from the "beautiful life needs to carefully record" blog, please be sure to keep this http://huing.blog.51cto.com/1669631/1294962

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.