Java String API-I don't know, I don't understand Java__java.

Source: Internet
Author: User
Tags function prototype stringbuffer
First, the prefaceIt is well known that no matter which programming language is used, there is always a lot of dealings with strings. If the right programming language is used in string processing, the API is full, it can save a lot of trouble. As for the current experience, Java is quite handy for string processing. This blog is mainly to give you a summary of Java, about string of those common APIs, in the future when you use, you can easily query.
second, the common API

Construction device

In Java, everything is object, string is also. If it is an object, the first function that comes to mind is naturally the constructor. The syntax is as follows:

String str = new String ("I am a lucky string."); Construction device
We know there is another way to initialize string
String str = "I am a lucky string"
Are there any differences between the two initialization methods? The answer is, of course, the difference is not small. However, this article in this respect will not repeat, want to understand the students can refer to this blog click to open link length () Length of a string, as shown below
System.out.println ("My length is" + str.length ()); Length ()
CharAt ()
char charAt (int index), returns the char at index subscript position in string, throws Indexoutofboundsexception exception if index is not valid. Examples are as follows:
System.out.println ("My favoriate character is" + Str.charat (8));  CharAt () output:u
GetChars
public void GetChars (int srcbegin, int srcend, char dst[], int dstbegin), copy the string from Srcbegin to Srcend, and then to the target string. Start copying from the subscript from the Dstbegin. Of course if subscript has an illegal, also can throw indexoutofboundsexception exception. Examples are as follows:
Char dst[] = {' A ', ' P ', ' o ', ' o ', ' r ', ' G ', ' I ', ' R ', ' L '};
		System.out.println ("Now I Want", "I lucky to a good guy");
		Str.getchars (7, DST, 0);    GetChars (), Output:luckygirl
GetBytes ()
Encodes a string using the platform default encoding and stores the result in a new byte array. Examples are as follows:
byte[] B_GBK = Str.getbytes ();  GetBytes ()
ToCharArray ()
Converts a string to a char array, as shown in the following example:
DST = Str.tochararray (); ToCharArray ()
System.out.println (DST);   Output:i am a lucky string.
Equals ()
public boolean equals (Object AnObject) compares whether the source string and anobject content are equal, noting that "=" compares whether the reference is equal. The difference between the two is not detailed in this article, please see Click Open link
Equalsignorecase ()
Usage is similar to equals (), except that the case is ignored when compared. CompareTo ()
public int CompareTo (string anotherstring), comparing the size of two strings in dictionary order Oh. Dictionary order is to say a<b<c, the return value has three kinds of possible: 1,0,-1 is greater than, equal to, less than. Examples are as follows:
if (Str.compareto ("I am a unlucky string.") > 0) {   //compareto (), output:i am smaller
	System.out.println ("I am b Igger ");
} else {
	System.out.println ("I am Smaller");
}
Contains ()
Boolean contains (Charsequence s) to determine if S is contained in the source string. The containing returns 1, not including the return 0. about what is charsequence, this article does not repeat, please own Baidu. Examples are as follows:
if (Str.contains ("lucky")) {                             //contains (), Output:<span style= "font-family:arial, Helvetica, Sans-serif;" >i contain lucky word</span>
	System.out.println ("I contain lucky word");
} else {
	System.out.println ("I don ' t contain lucky word");
}

Contentequals ()
Boolean contentequals (StringBuffer sb), method to compare strings to a specified charsequence. The result is true if and only if this string specifies a sequence of char values with the same sequence. Examples are as follows:
StringBuffer strbuf = new StringBuffer ("I am a lucky string.");
	if (Str.contentequals (strbuf)) {                             //contentequals (), output:the same System.out.println
		("the Same");
	else {
		System.out.println ("diffenent");
	}
Regionmatches ()
Boolean Regionmatches (boolean ignoreCase, int toffset, String other, int ooffset, int len). The first parameter, ignorecase, indicates whether the size needs to be ignored when comparing the string from the Toffset subscript and whether the string other from the following table Ooffset is equal, and Len represents the length of the specified comparison. Examples are as follows:
String strnew = new String ("I AM A LUCKY string.");
		if (Str.regionmatches (True, 7, Strnew, 7, 5)) {                             //regionmatches ()
			System.out.println ("the Same");  Output this line
		} else {
			System.out.println ("diffenent");
		}
StartsWith ()
Boolean startswith (string prefix) determines whether to start with prefix, returns True, and vice versa, returns False Boolean EndsWith (string suffix)
To determine whether to end with prefix, returns True, and vice versa, returns false
IndexOf ()
int indexOf (int ch), looking from left to right for a character with an ASCII code of CH, and returns the subscript for that character if it exists. If it does not exist, return-1
LastIndexOf ()
int indexOf (int ch), looking from right to left for a character with the ASCII code of CH, and returns the subscript for that character if it exists. If it does not exist, return-1. An example of indexof with the above is shown below:
System.out.println (str.indexof);          IndexOf ()     output: 2   
		System.out.println (Str.lastindexof ());         LastIndexOf () Output: 5
SUBSTRING ()
string substring (int beginindex, int endindex), returns the strings subscript from Beginindex to subscript endIndex-1. Examples are as follows:
System.out.println (str.substring (7));        SUBSTRING, output:	luck
Concat ()
Stitching two strings. Examples are as follows:
System.out.println (Str.concat ("Do I Like me?");        concat, Output: I am a lucky string. Do I like me?
Replace ()
String replace (char Oldchar, Char Newchar), as you can see from this function prototype, replace the Oldchar in string with Newchar. Is the replacement of all Oh, examples are as follows:
System.out.println (Str.replace (' A ', ' a '));        Replace, output: I Am A Lucky String.
toUpperCase () and toLowerCase ()
Do not explain, turn to uppercase/lowercase. Trim ()
When you delete a blank character at both ends of a string, a new string object is returned. If no change occurs, the original string object is returned. Examples are as follows:
strnew = "          I am a lucky string.            ";
		System.out.println (Strnew.trim ());        Trim, output: I am a lucky string.
ValueOf () returns a string representing the contents of the parameter, which can be double,int,float,char,char[],long and so on, basically. It's actually type conversion. Converts other types of data into strings. Examples are as follows:
System.out.println (str.valueof (8.8));      valueof Output: 8.8
Intern ()
Generates only one string reference for each unique sequence of characters. What does that mean. This means that a sequence of characters already exists, then returns a reference to the sequence of characters, as follows:
System.out.println (STR3 = = STR4);         Output: false
		STR4 = (str1 + str2). Intern ();            Emphasis: Intern ()
		System.out.println (str3 = = STR4);         Output: True
Third, the full version of the program
public class Stringclass {public static void main (string[] args) {string str = new String ("I am a lucky string."); Constructor System.out.println ("My length is" + str.length ());  Length () System.out.println ("My favoriate character is" + Str.charat (8));
		CharAt () output:u char dst[] = {' A ', ' P ', ' o ', ' o ', ' r ', ' G ', ' I ', ' R ', ' L '};
		System.out.println ("Now I Want", "I lucky to a good guy");    Str.getchars (7, DST, 0);
		GetChars (), Output:luckygirl System.out.println (DST);  byte[] B_GBK = Str.getbytes ();
		GetBytes () System.out.println (B_GBK); DST = Str.tochararray ();   ToCharArray () System.out.println (DST);
		
		Output:i am a lucky string. if (Str.equals ("I am a unlucky string.")
		{//equals () System.out.println ("the same");
		else {System.out.println ("diffenent"); } if (Str.equalsignorecase ("I AM A LUCKY STRING.")
		{//equalsignorecase () System.out.println ("the same");
		else {System.out.println ("diffenent"); } IF (Str.compareto ("I am a unlucky string.") > 0) {//compareto (), output:i am smaller System.out.println ("I am Bigge
		R ");
		else {System.out.println ("I am Smaller"); } if (Str.contains ("lucky")) {//contains () System.out.println ("I contain lucky word")
		;
		else {System.out.println ("I don ' t contain lucky word");
		} stringbuffer strbuf = new StringBuffer ("I am a lucky string."); if (Str.contentequals (strbuf)) {//contentequals (), output:the same System.out.println ("the
		Same ");
		else {System.out.println ("diffenent");
		String strnew = new String ("I AM A LUCKY string."); if (Str.regionmatches (True, 7, Strnew, 7, 5)) {//regionmatches () System.out.println ("The S
		Ame ");
		else {System.out.println ("diffenent");
		} if (Str.startswith ("I")) {//startswith () System.out.println ("I start with I"); else {System.Out.println ("I don ' t start with I"); if (Str.endswith ("string."))
		{//endswith () System.out.println ("I End with string.");
		else {System.out.println ("I don ' t end with string.");          } System.out.println (Str.indexof (97));         IndexOf () System.out.println (Str.lastindexof (97));        LastIndexOf () System.out.println (str.substring (7, 11));        SUBSTRING, Output: Luck System.out.println ("Do I Like me?"); concat, Output: I am a lucky string.	
		Do I like me?        System.out.println (Str.replace (' A ', ' a '));
		Replace, output: I Am A Lucky String.            System.out.println (Str.touppercase ());            toUpperCase strnew = "I am a lucky string."
		";        System.out.println (Strnew.trim ());
		Trim, output: I am a lucky string.      System.out.println (str.valueof (8.8)); 
		valueof output: 8.8 String str1 = "a"; 
		String str2 = "BC"; 
		String STR3 = "a" + "BC"; String STR4 = Str1+str2;         System.out.println (STR3 = = STR4);            Output: false STR4 = (str1 + str2). Intern ();         Emphasis: Intern () System.out.println (STR3 = = STR4); Output: True}}

If there are deficiencies, please note that.
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.