Java String class method in-depth parsing _java

Source: Internet
Author: User
Tags hash locale string format

Copy Code code as follows:

Import Java.nio.charset.Charset;
Import java.nio.charset.UnsupportedCharsetException;
Import Java.util.Locale;
Import Java.util.Date;
Import java.util.regex.PatternSyntaxException;

Import Javax.xml.crypto.Data;

public class Stringxuexi {
public static void Main (string[] argc)
{
charAt (int index) returns the Unicode character at index
String strcom = "Java programming";
System.out.println (Strcom.charat (4));

Codepointat (int index) returns the Unicode encoding value of the character at index
strcom = "I like JAVA, too";
System.out.println (Strcom.codepointat (8));

Codepointbefore returns the Unicode encoding value of the character at Index-1
System.out.println (Strcom.codepointbefore (2));

Codepointcount (int beginindex,int endindex) method returns the number of Unicode code points within the specified text range
System.out.println (Strcom.codepointcount (0, 3));


CompareTo (String str)
If two strings are different, they either have different characters at one index or different lengths, or both.
If the characters differ at one or more indices, assuming k is the minimum value for such an index, then the return value is the two strings at position K
The difference between two char values, and the return value is the difference of two string lengths if there are no different index positions for the characters
System.out.println (Strcom.compareto ("I like PHP"));
System.out.println (Strcom.compareto ("I like JAVA Too"));

Comparetoignorecase (string str) ignores case comparison string size
System.out.println (Strcom.comparetoignorecase ("I like PHP"));

Concat (String str) joins another string after this string, if the length of the argument string is 0,
This string is returned, or a new string object is created
System.out.println (Strcom.equals (Strcom.concat ("")));
System.out.println (Strcom.concat (strcom));

Contains (Charsequence s) determines whether a string contains a specified sequence of char values
System.out.println (Strcom.contains ("JAVA"));

valueof (char []data) static method that returns a string containing the characters of a character array
Char [] array={' mountain ', ' East '};
System.out.println (string.valueof (array));

valueof (char[] data,int offset,int count) returns the count characters that contain a character array starting at offset
A string composed of
System.out.println (string.valueof (array, 0, 1));

Endwith (string suffix) tests whether the string is the end of the specified suffix
System.out.println (Strcom.endswith ("JAVA"));

Equals (Object obj) returns True if the given object represents a string equal to this string, otherwise false
System.out.println (Strcom.equals ("I like JAVA"));

Equalsignorecase (string anotherstring)//Ignore case is compared to another string, noting that the argument type is different from the Equals method
System.out.println (Strcom.equalsignorecase ("I like JAva"));

Format (string format,object ... args) static method returns a format-word string using the specified format string and parameters
%d formatted as decimal integer
%o Format as octal integer
The%x%x is formatted as a hexadecimal integer

System.out.println (String.Format ("%e%x%o%d%a%%%n", 15.000,15,15,15,15.0));


Format (Locale l,string format,object ... args)
Formats a date and time string with a given special converter as a parameter
%te one day in one months
%TB the month abbreviation for the specified locale
%TB the month full name of the specified locale
%ta the weekday name of the specified locale
%ta the weekday abbreviation of the specified locale
%TC includes full date and time information
%ty 4-bit year
%ty Two-bit year
%tm Month
%TJ the first day of the year
%TD the first day of one months
Date date = new Date ();
Locale form = Locale.china;
String year = String.Format (form, "%ty", date);
String month = String.Format (form, "%tm", date);
String day = String.Format (form, "%TD", date);
System.out.println ("Today is:" + year + "Years" +month+ "month" +day+ "Day");
System.out.println (String.Format (Form, "%TC", date);

Byte[] GetBytes () gets the byte sequence of the string
byte[] str = strcom.getbytes ();
for (int i = 0;i < str.length;i++)
System.out.print (str[i]+ "");

GetBytes (Charset Charset)
GetBytes (String string)
Gets the sequence of characters derived from the coded character set
try {
str = strcom.getbytes (Charset.defaultcharset ());
for (int i = 0; i < str.length; i++)
System.out.println (Str[i] + "");
catch (Unsupportedcharsetexception e) {
Todo:handle exception
E.printstacktrace ();
}

GetChars (int srcbegin,int srcend,char[] Dst,int dstbegin)
Copy a character from this string to a target character array
char[] DST = new CHAR[10];
Strcom.getchars (0, DST, 0);
for (int i = 0; i < dst.length;i++)
System.out.print (Dst[i]);
System.out.println ();

Hashcode () returns the hash code of the string, which is the calculation formula for the hash code
s[0]*31^ (n-1) +s[1]*31^ (n-2) +...+s[n-1]
The hash code for the empty string is 0
System.out.println (Strcom.hashcode ());

indexOf (int ch) Gets the first index of the character, CH is a character, if not, returns-1
System.out.println (Strcom.indexof (' A '));

indexOf (int ch,int fromindex)//Returns the index of the specified character starting at the specified index
Fromindex No limit, if negative, equivalent to 0, if greater than or equal to string length, return-1
System.out.println (Strcom.indexof (' A ', 9));

IndexOf (String str)
IndexOf (String str,int fromindex)
Returns the index of the specified string at the first occurrence of this string
System.out.println (Strcom.indexof ("JAVA"));

Intern () Returns a normalized representation of a string object
When the Intern method is invoked, if the pool already contains a string equal to this string object, the string in the pool is returned
Otherwise, this string object is added to the pool, and returns a string object reference
Understanding this processing mechanism also allows us to learn how to save the memory that these strings occupy when we use string constants.
String strCom2 = new String ("I like JAVA");
System.out.println (strcom = = strCom2);
System.out.println (Strcom.endswith (STRCOM2));
System.out.println (Strcom.compareto (STRCOM2));
System.out.println (strcom.intern () = = Strcom2.intern ());
string S1 = new String ("Hello, Java free man");
String s2 = new String ("Hello,") + "Java free person";
System.out.println (S1==S2);
System.out.println (S1.intern () ==s2.intern ());

With indexof, note the Fromindex parameter, refers to the reverse search from Fromindex
System.out.println (Strcom.lastindexof (' A '));
System.out.println (Strcom.lastindexof (' A ', 10));
System.out.println (Strcom.lastindexof ("JAVA"));
System.out.println (Strcom.lastindexof ("JAVA", 10));

return string length
System.out.println (Strcom.length ());

Matchs (String regex) match regular expression
try {
String regex = "1234";
System.out.println (Regex.Matches ("//d{4}"));
System.out.println (Regex.replaceall ("//d{4}", "Chen"));
System.out.println (Regex.replacefirst ("//d{4}", "Chen"));
catch (Patternsyntaxexception e) {
Todo:handle exception
E.printstacktrace ();
}

offsetbycodepoints (int index,int codepointoffset)
Returns the index of the Codepointoffset code point offset from the given index
System.out.println (Strcom.offsetbycodepoints (7, 4));

Tests whether two string regions are equal, and the first argument to true indicates that the case is ignored
System.out.println (Strcom.regionmatches (True, 0, "I like", 0, 3));
System.out.println (strcom.regionmatches (0, "I like", 0, 3));

System.out.println (Strcom.replace (' A ', ' a '));
System.out.println (Strcom.replace ("JAVA", "PHP"));

String[] Split (String regex,int limit)
Splits the string content by the specified separator and puts it in a string array, limit the number of times the control mode is applied
String[] Info = strcom.split (",");
for (int i = 0; i < info.length;i++)
System.out.println (Info[i]);

info = Strcom.split ("", 2);
for (int i = 0; i < info.length;i++)
System.out.println (Info[i]);

StartsWith (String prefix,int toffset)//To determine whether to start with the specified prefix
Toffset is negative or greater than the string length result is False
System.out.println (Strcom.startswith ("I"));
System.out.println (Strcom.startswith ("I",-1));

Charsequeuece subsequeuece (int beginindex,int endindex)
Returns a new sequence of characters
System.out.println (Strcom.subsequence (2, 6));

String substring (int beginindex,int endindex)
Back substring
System.out.println (strcom.substring (2));
System.out.println (Strcom.substring (2, 6));

ToCharArray () string variable character array
char[] str1 = Strcom.tochararray ();
for (int i = 0; i < str1.length;i++)
System.out.print (str1[i]+ "");
System.out.println ();

toLowerCase (Locale Locale) converts all characters in a string into large/lowercase returns a new string
System.out.println (Strcom.tolowercase ());
System.out.println (Strcom.touppercase ());
System.out.println (Form) (strcom.touppercase);
System.out.println (Form) (strcom.tolowercase);

Trim () method to remove the front and back whitespace of a string
System.out.println (("" +strcom). Trim ());

valueof () static method implements the basic data type into a string
System.out.println (String.valueof (true));
System.out.println (string.valueof (' A '));
System.out.println (string.valueof (12.0));
}
}

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.