String, strngbuffer, stringbulider

Source: Internet
Author: User

1. The Sting class refers to the description of string transactions, which defines methods specifically used to operate strings. It is a final class and cannot be inherited.

String S1 = "Java"; strings2 = "Java" the string class adopts the metadata design mode in Java. The reference variables S1 and S2 point to the same memory address. S1 = S2 is true. string S = new string ("Java"); two objects are created. "Java" is an anonymous object, and the New Keyword creates another object in the memory.

Ii. List common methods of the string type

1. Get.
The number of characters contained in the 1.1 string, that is, the length of the string.
Int length (): Get the length.
1.2 obtain a character at the position based on the position.
Char charat (INT index ):
1.3 obtain the position of the character in the string based on the character.
Int indexof (INT ch): return the location where ch first appears in the string.
Int indexof (int ch, int fromindex): obtains the position where ch appears in the string starting from the position specified by fromindex.
Int indexof (string Str): return the position where STR first appears in the string.
Int indexof (string STR, int fromindex): obtains the position where STR appears in the string starting from the position specified by fromindex.
Int lastindexof (INT ch): obtains the position of the first occurrence of a character from the end of the string.

2. Judgment.
2.1 Boolean contains (STR): indicates whether the string contains a substring.
Special: indexof (STR): You can index the position where STR appears for the first time. If-1 is returned, this STR does not exist in the string. Therefore, it can also be used to determine whether to include the specified content.
If (Str. indexof ("AA ")! =-1, and this method can be used to determine and obtain the location where it appears.
Whether the content contains 2.2 characters.
Boolean isempty (): determines whether the length is 0.
2.3 whether the string starts with the specified content.
Boolean startswith (STR );
2.4 whether the string ends with the specified content.
Boolean endswith (STR );
2.5 determine whether the string content is the same. Describes the equals method in the object class.
Boolean equals (STR );
2.6 determine whether the content is the same and ignore the case.
Boolean inclusignorecase ();

3. Conversion.
3.1 convert the character array into a string.
Constructor: string (char []) string (char [], offset, count): convert a part of the character array into a string.
Static Method:
Static string copyvalueof (char []);
Static string copyvalueof (char [] data, int offset, int count)
Static string valueof (char []):
3.2 convert a string into a character array: Char [] tochararray ():
3.3 convert the byte array into a string.
String (byte [])
String (byte [], offset, count): convert part of the byte array into a string.
3.4 convert a string into a byte array.
Byte [] getbytes ():
3.5 convert the basic data type to a string.
Static string valueof (INT)
Static string valueof (double) // 3 + "; // string. valueof (3 );

Special: encoding tables can be specified during conversion of strings and byte arrays.
The constructor is Public String (byte [] bytes, charset). A new string is constructed by decoding the specified byte array using the specified charset.

4. Replace: String Replace (oldchar, newchar); // if the character to be replaced does not exist, the original string is returned.
5. Cut: String [] Split (RegEx );
6. substring. Obtain a part of the string.
String substring (BEGIN); // starts from the specified position and ends
String substring (begin, end); // contains the header, not the end.

7. Convert, remove spaces, and compare.

7.1 convert the string to uppercase or lowercase.
String touppercase ();
String tolowercase ();
7.2 remove multiple spaces at both ends of the string.
String trim ();
7.3 compare two strings in natural order.
Int compareto (string );

Iii. Exercises

Public class stringtest1 {/*** 1. remove the spaces at both ends of the string * 2. write a string inversion function */public static void main (string [] ARGs) {system. out. println (mytrim ("DSDs DSD dsok kp"); system. out. println (reversestring ("Java Hello World"); system. out. println (getsubcount ("fjfdfffjkkjkfffkjkkjkjff", "FF");} public static string mytrim (string s) // empty header and Tail removal {/** train of thought: 1. judge from the beginning until the position of a non-null character is found. judge from the end until the position B * 3 where non-null characters are found. truncates A to B Sub-string **/INT start = 0, end = S. length ()-1; while (start <= end & S. charat (start) = '') Start ++; while (start <= end & S. charat (end) = '') end --; return S. substring (START, end + 1);}/** string inversion ideas: * 1. convert a string to a character array * 2. define two variables pointing to the beginning and end of the character array respectively * 3. the variable pointing to the header variable moves backward, and the variable pointing to the end moves forward, switching in turn * 4. convert the reversed character array into a string ***/public static string reversestring (string S, int X, int y) // reverse the string {char ch [] = S. tochararray (); For (INT start = 0, end = S. length ()-1; Start <end; Start ++, end --) {swap (CH, start, end);} return new string (CH);} public static void swap (char [] CH, int A, int B) // Switch location {char temp = CH [a]; ch [a] = CH [B]; ch [B] = temp ;} public static string reversestring (string s) // reload the string Reverse Method {return reversestring (S, 0, S. length ()-1);} // gets the number of times a string appears in another string. Public static int getsubcount (string S, string key) {int COUNT = 0, Index = 0; while (Index = S. indexof (Key, index ))! =-1) {Index = index + key. Length (); count ++;} return count ;}// gets the number of times a string appears in another string. Public static int getsubcount_1 (string S, string key) {int COUNT = 0, Index = 0; while (Index = S. indexof (key ))! =-1) {S = S. substring (index + key. Length (); count ++;} return count ;}}
/* Obtain the maximum number of identical substrings of two strings? Idea: 1. compare the length of two strings. select a short string and obtain its child string that is gradually reduced by length, starting from the maximum string, such as string: ABCDE Length 5 --- 1 ABCDE length 4 --- 2 ABCD bcde length 3 --- 3 abc bcd cde length 2 --- 4 AB bc cd de length 1 --- 5 a B c d E 3. cyclically determine whether a long string contains the obtained string */class stringtest3 {public static void main (string [] ARGs) {string S1 = "ohello "; string S2 = "cfdkhel lkhellokiohello"; // system. out. println (getmaxsubstring (S1, S2); getstring ("ABCDE");} public static string getmaxsub String (string S1, string S2) {string max = s1.length ()> s2.length ()? S1: S2; string min = s1.length () <s2.length ()? S1: S2; For (INT x = 0; x <min. length (); X ++) {for (INT y = 0, Z = min. length ()-X; Z <min. length () + 1; y ++, Z ++) {string temp = min. substring (Y, Z); // substring does not take the Z character, so pay attention to the above parameter value if (max. contains (temp) {return temp ;}} return "" ;}// obtain the public static void getstring (string S) of the substring that increases by length) {for (INT x = 0; x <S. length (); X ++) {for (INT y = 0, Z = x + 1; Z <= S. length (); y ++, Z ++) {system. out. println (S. substring (y, z ));}}}}

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.