JAVA learning lesson 30th (Common Object API)-String class: Class Method exercises, apistring

Source: Internet
Author: User

JAVA learning lesson 30th (Common Object API)-String class: Class Method exercises, apistring


Intern Method

Public class Main {public static void main (String [] args) {String str1 = new String ("asd"); String str2 = str1.intern ();/* The String constant pool has, returns the string. If no string is returned, the System creates */System. out. println (str2); System. out. println (str1 = str2 );}}

Exercise 1: Sort string Arrays

Import java. util. substring;/** specify a String array in ascending Lexicographic Order */public class Main {public static void main (String [] args) {stringsort ();} public static void stringsort () {String [] str = new String [10]; exist in = new expect (System. in); for (int I = 0; I <str. length; I ++) {str [I] = in. nextLine ();} // System. out. println (str. length); for (int I = 0; I <str. length-1; I ++) {for (int j = 0; j <str. length-1-I; j ++) {if (str [j]. compareTo (str [j + 1])> 0) {String t = str [j]; str [j] = str [j + 1]; str [j + 1] = t ;}}for (String iString: str) {System. out. println (iString );}}}

Exercise 2: count the number of times the substring appears in the parent string

IndexOf application, record the current subscript A, and then continue from the length of A + substring after indexOf, Use loop to achieve this function

Import java. util. counts;/** number of times a substring appears in a String */public class Main {public static void main (String [] args) {count ();} public static void count () {String str = "abcdefabcghiabcsdabchzabc"; int num = 0, wz = 0; for (int I = 0; I <str. length (); I ++) {int t = str. indexOf ("abc", wz); if (t! =-1) {wz = t + "abc". length (); num ++;} else break;} System. out. println (num );}}

Exercise 3: Maximum and same substrings of two strings

Import java. util. longest;/** the longest public substring */public class Main {public static void main (String [] args) {String tString = compare (); System. out. println (tString);} public static String compare () {String str1 = "vbabcdefgsdfg"; String str2 = "asdabcdefgdf"; int Mlen = str1.length (); int Zlen = str2.length (); int len = (Mlen> Zlen )? Zlen: Mlen; // find a shorter parent string. The maximum public length cannot exceed len // The Public substring may appear in the middle for (int I = 0; I <len; I ++) // control the maximum length {for (int j = 0, k = len-I; k <= len; k ++, j ++) // j controls several conditions, and k controls the substring's final element subscript not to cross-border {String sub = str2.substring (j, k); if (str1.contains (sub) return sub ;}} return null ;}}

Exercise 4: Remove spaces at both ends (trim simulation)

public class Main {public static void main(String[] args) {String string = "    asd fs fg h   ";int st = 0,en = string.length()-1;for(;st<=en && string.charAt(st)==' '; st++);for(;st<=en && string.charAt(en)==' '; en--);String sub = string.substring(st, en+1);System.out.println(sub);}}



Several common methods for converting JAVA objects to Java strings

This article will summarize the commonly used conversion methods. Common methods include Object # toString (), (String) Object to be converted, and String. valueOf (Object. These methods are analyzed one by one. Method 1: Use the Object # toString () method. See the following example: Object object = getObject (); System. out. PRintln (object. toString (); in this method, Because java. lang. the public method already exists in the Object class. toString (), so this method can be called for any strictly-defined java object. However, you must ensure that the object is not null. Otherwise, an NullPointerException will be thrown. When this method is used, the derived class usually overwrites the toString () method in the Object. Method 2: Use the type conversion (String) object method. This is a standard type conversion. convert an object to a value of the String type. When using this method, you must note that the type must be converted to the String type. Therefore, it is best to use instanceof for a type check to determine whether conversion is allowed. Otherwise, a CalssCastException is thrown. In addition, it is important to note that syntax check does not report errors when an Object defined as an Object is converted to a String, which may lead to potential errors. Be especially careful. For example, Object obj = new Integer (100); String strVal = (String) obj; an error occurs during runtime because the Integer type cannot be forcibly converted to the String type. However, Integer obj = new Integer (100); String strVal = (String) obj; if the format code is used, a syntax error is returned. In addition, because the null value can be forcibly converted to any java class type, (String) null is also valid. Method 3: Use String. valueOf (Object) String. valueOf (Object) Based on Object # toString (). But it is different from Object # toString. As mentioned in the analysis in method 1 above, when using the latter, you must ensure that it is not null. However, when using the third method, you don't have to worry about whether the object is null. For ease of Problem description, let's analyze the relevant source code. In Jdk, the String # valueOf (Object) source code is as follows:/*** Returns the string representation of the Object argument. ** @ param obj an Object. * @ return if the argument is null, then a string equal to * "null"; otherwise, the value of * obj. toString () is returned. * @ see java. lang. object # toString () */public static String valueOf (Object obj) {return (obj = null )? "Null": obj. toString ();} from the source code above, we can clearly see the reason why the null value does not need to be worried. However, this also gives us hidden risks. We should note that when the object is null, the value of String. valueOf (object) is the String "null", rather than the remaining full text>

In Java, what are common StringBuilder APIs, Integer APIs, and Double APIs?

Search for "Java jdk api 1.6.0 Chinese Version documentation" on baidu and download the api.
To learn java, you must learn to view APIs. Otherwise, you will always follow others.

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.