Java Interview Preparation String Class special breakthrough + source Analysis

Source: Internet
Author: User
Tags comparable first string

There are many arrays methods used in the source code of string, it is recommended to refer to the Arrays Class library first.

Basic Introduction:

String is a very special class, and there are many ways to build it.

If you use a traditional construction method such as String s = new String ("string"), then the object will be allocated on the heap, when the comparison of two string address is not equal, and "" double quotation marks wrapped up in the constant pool of content will stay, if there are two content the same address is the same.

Therefore, it is not reliable to use = = to compare strings.

The String class also implements three interfaces: Serializable, Charsequence, comparable<string>.

The serializable is just a serializable token, not to be used to control it.

Charsequence This interface provides three important methods for string:

charAt (int index), which returns the value of the index

Length (), which returns the string sequence lengths

subsequence (int start,int end), look at the name to see, return a subsequence

Comparable is a very important interface (see its subclasses to know), function here to carry the text in the following Javadoc.

Role: This interface forces the overall ordering of objects that implement each of its classes. This sort is called the natural ordering of the class, and the compareTo method of the class is called its natural comparison method.

    This interface can be used to sort the collection or array, and naturally, the ordering of the strings is also indispensable.

Only one CompareTo () is implemented.

Class Library use:

Java in the class library of each class is too much, in this do not want to do a simple list, we try to design a class thinking way to familiar with some methods, and then cooperate with the source of the analysis, it should be understood more clearly.

Assuming that we are the Java language Designer (not so formal), now that we have implemented three interfaces for the string class, the first thing is to rewrite these interfaces.

Grasp the principle of string essence or char array,

The charAt (int index) method is rewritten as a method to get the index character in the array

The length () method returns the size of the array, when it is easy to memorize two "lengths", and string calls a method so it is length (), and this method calls an attribute of the array length

The Subsequence () method was rewritten to call the substring method, and after a series of bounds checks in the substring method, a constructor method was called, and the Arrays.copyofrange () method was called.

CompareTo is the most complex one, and it implements a comparison of two strings. It loops over the first different characters and returns their difference, and returns 0 if they are all the same. This method looks to the outside. The string dictionary order is less than the string being compared, returns a number less than 0, and returns a number greater than 0;

    
1   Public intcompareTo (String anotherstring) {2         intLen1 =value.length;3         intLen2 =anotherString.value.length;4         intLim =math.min (Len1, len2);5         CharV1[] =value;6         CharV2[] =Anotherstring.value;7 8         intK = 0;9          while(K <Lim) {Ten             CharC1 =V1[k]; One             CharC2 =V2[k]; A             if(C1! =C2) { -                 returnC1-C2; -             } thek++; -         } -         returnLEN1-Len2; -}
View Code

Now that we've implemented all the interface methods, it's time to do a little sting yourself.

The first is the construction method, which is not mentioned here, but a lot of construction methods use the static method of Arrays.copy ().

Then it should be judged:

is empty: IsEmpty ()

Whether to start or end with a specific string: Startwith (), Endwith ()

Whether to include a string: Contains (charsequence s), note the parameter, we can still use string instead

is equal to a string: Equals (object), before the source object is converted to string, using instanceof to determine whether the object is of type string and avoids throwing an exception.

Whether it is equal to a string (regardless of case): Equalsignorecase (), basic all case-insensitive usage adds a suffix

Then it should be split, merge:

Replace: Replacement character: replace (char old, char new)

        Replacement string:replace(CharSequence target, CharSequence replacement)

         替换所有字符串,但是第一个参数是一个正则表达式:replaceAll(String regex, String replacement)

Replace the first string, but the first argument is a regular expression:replaceFirst(String regex, String replacement)

Split: Splits a string by the specified regular expression, which is an array of strings:split(String regex)

Returns a substring:substring(int beginIndex, int endIndex)

Ignore the blanks before and after:trim()

Merge: Concatenate the specified string to the end of this string:concat(String str)

Convert: Convert an array to a string:copyValueOf(char[] data)

To convert a string to an array:toCharArray()

Case conversion:toLowerCase(),toUpperCase()

Convert various types to strings: variousvalueOf(Any b)

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.