Java Learning Note 02

Source: Internet
Author: User
Tags control characters locale modifier printable characters

A string class of generic strings

    • Declaring a string

>>string Str

    • Create a string

The >>string (char a[]) method is used to create a character array as a String object

The >>string (char a[],int offset,int length) method is used to extract a portion of the character array A to create a string object, where the parameter offset represents the position at which the string is to be truncated, and length indicates how long to intercept the string

The >>string (char a[] value) method can be used to assign a new String object that represents the result of all object joins in the character array parameter

    • Connection string

>> Use the "+" operator to implement the ability to concatenate multiple strings, or you can concatenate multiple operators and produce a string object

>> Use the "+" operator to connect to other base data types and convert other data types to strings

    • Get string information

>> use the length () method of the string class to get the lengths of the declared string objects

    • String Lookup

The >>indexof (string s) method is used to return the index position of the first occurrence of the argument string s in the specified string. When the method is executed, the position of S is searched from the beginning of the string, and if the string s is not retrieved, the return value of the method is-1

The >>lastindexof (String str) method is used to return the index position of the last occurrence of the specified string, which is the same as indexof ()

    • Gets the character at the specified index position

The >>charat (int index) method returns the character of the specified index position to the

    • Get substring

The >>substring (int beginindex) method is used to get the substring that is intercepted at the end of the string starting at the specified index position

The >>substring (int beginindex,int endIndex) method is used to get a substring that is intercepted at a specified position and ends at an index position

    • Remove spaces

The >>trim () method returns a copy of the string, ignoring leading and trailing spaces

    • String substitution

The >>replace (char Oldchar,char Newchar) method replaces the specified character or string with a new character or string, and returns the original string if there is no target string

    • Judging the start and end of a string

The >>startswith (string prefix) method and the EndsWith (string suffix) method are used to determine whether a string starts or ends with the specified content, and the return value is of type Boolean

    • Determine if strings are equal

>> "= =" is used to compare the addresses of two strings, even if the content length is exactly the same, the address of two strings may be different

The >>equals (string otherstr) method is used to compare the contents and length of two strings, returning true when identical

The >>equalsignorecase (string otherstr) method is used to compare the contents and length of two ignored case strings, returning true when identical

    • Compare two strings in a dictionary order

The >>compareto (string otherstr) method compares two strings in a dictionary order. Starting from the first bit of the two strings, if the characters of the first bit of the two string are the same, the backward comparison continues until a different character is present. If the last one is the same (that is, the Equals (object) method returns a value of true), the result is 0, and if different, the string object precedes the argument string, the result of the comparison is a negative integer, and vice versa is a positive integer

    • Letter size Conversion

The >>tolowercase () method is used to convert a string to lowercase

The >>touppercase () method is used to convert a string to uppercase

    • String segmentation

The >>split (string sign) method splits the contents of a string by a specified split character or string and stores the split result in a string array

The >>split (String sign,int limit) method acts the same as above, but can limit the number of splits

Second, date and time string formatting

    • Date formatting
commonly used date format translator
translator Description example
%te one day in one months (1~31) 2
%tb Specify the month abbreviation for the locale Feb (English), February (Chinese)
%TB Specifies the full month of the locale February (English), February (Chinese)
%ta Specifies the day of the week for the locale Monday (English), Monday (Chinese)
%ta Specifies the weekday abbreviation for the locale Mon (English), Monday (Chinese)
%TC includes all date and time information Monday Two or Three month 13:37:22 CST.
%ty 4-bit year to
%TJ the day of the Year (001~366) 085
%tm month a
%td the day of the one month (01~31) Geneva
%ty 2-bit year to
    • Time formatting
time-formatted conversion characters
Conversion character Description Example
%th 2-digit 24 o'clock hour (00~23) 14
%ti 2-digit 12 o'clock hour (01~12) 05
%tk 2-digit 24 o'clock hour (0~23) 5
%tl 2-digit 12 o'clock hour (1~12) 10
%tm 2-digit minute (00~59) 05
%ts Number of seconds in 2 digits (00~60) 12
%tl Number of milliseconds in 3 digits (000~999) 920
%tn 9-digit subtle number (000000000~999999999) 062000000
%tp Specify the morning or afternoon markers for the locale Afternoon (Chinese), PM (English)
%tz Digital time zone offset relative to GMT RFC 82 format +0800
%tz A string in the form of a time zone abbreviation Cst
%ts 1970-01-01 00:00:00 to now the number of seconds elapsed 1206426646
%tq 1970-01-01 00:00:00 to now the number of milliseconds elapsed 1206412131231
    • Formatting common date and time combinations
Common format for date and time combinations
Conversion character Description Example
%tf "Year-month-day" format (4-bit year) 2008-03-25
%td "Month/day/year" format (2-bit year) 03/25/08
%tc All date and time information Monday Two or Three 15:20:00 CST 2008
%tr Time: minute: PM (AM) format (12 o'clock) 03:22:06 pm
%tt "Hours: minutes: Seconds" format (24 o'clock) 15:23:50
%tr "Time: Minute" format (24 o'clock) 15:25

Third, the general type of formatting

General translator
translator Description example
%b,%b results are formatted as Boolean types true
%h,%h results are formatted as walk codes a05a5198
%s,%s result is formatted as a string type "ABCD"
%c,%c results are formatted as character types ' a '
%d results are formatted as decimal integers +
%o results are formatted as octal integers one
%x,%x results are formatted as hexadecimal integers 4b1
% E results are formatted as decimal numbers in computer science notation 1.700000e+01
% A result is formatted as a hexadecimal floating-point value with significant digits and exponent 0x1.c000000000001p4
%n result is a line delimiter specific to the platform  
% result is literal% %

Four, the regular expression

>> regular expressions are often used to determine whether a statement satisfies a format. A regular expression is a string of characters with a special meaning that Fu Shi a meta-character called a regular expression.

metacharacters in regular expressions
Metacharacters The notation in regular expressions Significance
. . Represents any one character
\d \\d Represents any number of 0~9
\d \\d Represents any one non-numeric character
\s \\s Represents a blank character, such as ' \ t ', ' \ n '
\s \\s Represents non-whitespace characters
\w \\w Represents a character that can be used as an identifier, but does not include "$"
\w \\w Represents a character that cannot be used as an identifier
\p{lower} \\p{lower} Represents the lowercase letter A~z
\p{upper} \\p{upper} Represents the capital letter A~z
\P{ASCII} \\P{ASCII} ASCII characters
\p{alpha} \\p{alpha} Alphabetic characters
\p{digit} \\p{digit} decimal number, i.e. 0~9
\p{alnum} \\p{alnum} numeric or alphabetic characters
\P{PUNCT} \\P{PUNCT} Punctuation:! " #$%& ' () *+,-;:<=>[email protected][\]^_ ' {|} ~
\p{graph} \\p{graph} Visible characters: [\p{alnum}\p{punct}]
\p{print} \\p{print} printable characters: [\p{graph}\x20]
\p{blank} \\p{blank} Space or tab: [\ t]
\p{cntrl} \\p{cntrl} Control characters: [\x00-\x1f\x7f]

>> qualifier modifier for qualifying occurrences of metacharacters in regular expressions

Qualifier modifier
Qualifier modifier Significance Example
? 0 or 1 times A?
* 0 or more times A *
+ 1 or more times A +
N Happens to happen n times A{2}
{N,} appears at least n times A{3,}
{N,m} Appears n~m times a{2,6}

>> regular expression matching using the matches (String regex) method

Five, StringBuilder class

The >string class creates an object whose length is fixed and the content cannot be changed and compiled, although using "+" can achieve the purpose of attaching a new character or string, but "+" produces a new string instance that creates a new strings object in memory. To solve this problem, the system introduces the variable character sequence StringBuilder class, which greatly improves the efficiency of increasing the string frequently.

The >>append (content) method is used to append content to the string generator, enabling the acceptance of any type of data

The >>insert (int offset arg) method is used to insert data content into the string generator at the specified location

>>delete (int start,int end) method to remove a string that begins at a specified position to the end of a position

Java Learning Note 02

Related Article

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.