Dark Horse programmer-Java basics ------ string class

Source: Internet
Author: User

String:

1: PublicFinalClassStringExtends object

String S1 = "ABC ";// S1 is a class type variable and "ABC" is an object. // The biggest character of a string: it cannot be changed once initialized.

String S2 = new string ("ABC ");// S2 is two objects. S2 and ABC are the differences between S1 and S2.

String S3 = "ABC ";

System. Out. println (S1 = S2 );// Two objects: false

System. Out. println (s1.equals (S2 ));// String class describes the equals method in the object class. This method is used to determine whether the string address is the same. True

System. Out. println (S1 = S3 ));True

2: The string class is a description of a string. This class defines methods specifically used to operate strings. Common Operations are as follows:

Obtain:

The number of characters contained in a string, that is, the length of the string." Int length (): Get the length.

Obtain a character from a location." Char charat (INT index ):

Obtain the position of the character in the string according to 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 ):

Judgment:

Whether the string contains a substring." Boolean contains (STR ):

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 or obtain the position where it appears.

Whether there is content in the characters." Boolean isempty (): determines whether the length is 0.

Whether the string starts with the specified content." Boolean startswith (STR );

Whether the string ends with the specified content." Boolean endswith (STR );

Determines whether the string content is the same. The equals method in the object class." Boolean equals (STR );

Determine whether the content is the same, and ignore the case." Boolean inclusignorecase ();

Conversion:

Convert character arrays into strings."

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 []):

Convert a string into a character array." Char [] tochararray ():

Convert byte arrays into strings." String (byte [])

Convert part of the byte array into a string." String (byte [], offset, count ):

Convert a string into a byte array." Byte [] getbytes ():

Convert the basic data type to a string."

Static string valueof (INT)

Static string valueof (double)

Special: encoding tables can be specified during conversion of strings and byte arrays.

Replace:

String Replace (oldchar, newchar );

Cutting:

String [] Split (RegEx );

Substring. Obtain a part of the string.

String substring (BEGIN); string substring (begin, end );

Convert, remove spaces, and compare.

Converts a string to uppercase or lowercase. String touppercase (); string tolowercase ();

Remove multiple spaces at both ends of the string: "string trim ();

Compares two strings in natural order." Int compareto (string );

Remove space:

Public static stringMytrim (string Str){

Int start = 0, end =Str. Length ()-1; // The length minus a positive value is the badge of the last character.

While (start <= end & Str. charat (start) = '')

Start ++;

While (start <= end & Str. charat (end) = '')

End --;

Return Str. substring (START, end + 1 );// This method extracts the content containing the header and does not contain the end, so add 1 to the end.

}

String inversion:

Public static string reversestring (string S, int start, int end ){

// String variable array.

Char [] CHS = S. tochararray ();

// Reverse the array.

Reverse (CHS, start, end );

// Convert the array into a string.

Return new string (CHS );

}

Public static string reversestring (string s ){

Return reversestring (S, 0, S. Length ());

}

Private Static void reverse (char [] arr, int X, int y ){

For (INT start = x, end = Y-1; Start <end; Start ++, end --){

Swap (ARR, start, end );

}

}

Private Static void swap (char [] arr, int X, int y ){

Char temp = arr [x];

Arr [x] = arr [y];

Arr [y] = temp;

}

Returns the number of times a string appears in another string.

Public static int getsubcount_1 (string STR, string key ){

Int COUNT = 0; int Index = 0;

While (Index = Str. indexof (key ))! =-1) {// if the key is not found, it will be searched cyclically.

Sop ("str =" + Str );

STR = Str. substring (index + key. Length ());//After finding the location where the key appears for the first time, and adding the length of the key, you can find the location where the key appears for the first time.

Count ++;

}

Return count;

}

Or below

Public static int getsubcount_2 (string STR, string key ){

Int COUNT = 0, Index = 0;

While (Index = Str. indexof (Key, index ))! =-1 ){

Sop ("Index =" + index );

Index = index + key. Length ();

Count ++;

}

Return count;

}

Obtain the maximum number of identical substrings in two strings:Obtain the short substring in descending order of length. Determine whether or not to include each obtained substring in a long string. If so, it is found !.

Public static string getmaxsubstring (string S1, string S2 ){

String max = "", min = "";

Max = (s1.length ()> s2.length ())? S1: S2;

Min = (max = S1 )? S2: S1;

// Sop ("max =" + MAX + "... min =" + min );

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 );

Sop (temp );

If (max. Contains (temp) // If (s1.indexof (temp )! =-1)

Return temp;

}

}

Return "";

}

Stringbuffer:It is a string buffer and a container.

Features: The length can be changed. Multiple data types can be operated in bytes. The tostring method is used to convert the data into a string.

Function:

Storage:

Stringbuffer append (): add the specified data as a parameter to the end of the existing data.

Stringbuffer insert (index, data): You can insert data to the specified index position.

Delete:

Stringbuffer Delete (START, end): delete data in the buffer, including start, not end.

Stringbuffer deletecharat (INDEX): delete a character at a specified position.

.

Char charat (INT index)

Int indexof (string Str)

Int lastindexof (string Str)

Int length ()

String substring (INT start, int end)

Modify:

Stringbuffer Replace (START, end, string );

Void setcharat (INT index, char ch );

Reverse: stringbuffer reverse ();

Store the specified data in the buffer to the specified character array: void getchars (INT srcbegin, int srcend, char [] DST, int dstbegin)

After jdk1.5, stringbuilder is displayed:Stringbuffer is used for thread synchronization. Stringbuilder does not synchronize threads.Stringbuilder is recommended.

Basic Data TypeObject Packaging

The most common function of the basic data type object packaging class is to convert between the basic data type and the string type.

1:Convert the basic data type to a string.

Basic data type. tostring (Basic Data Type value); integer. Tostring (34 );

2:Converts a string to a basic data type.

Xxx a = xxx. parsexxx (string );Double B = double. Parsedouble ("12.23 ");// A numeric string must be input.

Integer I = new INTEGER ("123"); int num = I. Intvalue ();

3: Convert decimal to other hexadecimal formats:Tobinarystring (); tohexstring (); tooctalstring ();

4:Convert other hexadecimal values to decimal values:Parseint (string, Radix );

New Features of object packaging:

Integer x = new INTEGER (4 );

Integer x = 4; // automatically boxed. Equivalent to new INTEGER (4)

X =X/* Equivalent to X. intvalue ()*/+ 2;//X + 2: xAutomatic unpacking. To the int type. And 2. Then proceedPackingTo X.However, new features include:

One weakness is integer x = NULL. This is not acceptable because it points to Null x. intvalue () causes a null pointer exception. Therefore, you must determine whether to bind or detach a pointer first.

Disconnected?IsNull,

 


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.