Java Learning Lesson 30th (often using object APIs)-String class: Class method Exercises

Source: Internet
Author: User


Intern method

public class Main{public static void Main (string[] args) {string str1 = new String ("ASD"); String str2 = Str1.intern ();/* string constant in the pool, returns a string without creating a */system.out.println (STR2); System.out.println (str1 = = str2);}}

Exercise 1: Sorting a string array

Import java.util.scanner;/* * Given an array of strings, sorted ascending by dictionary order */public class main {public static void Main (string[] args) {Stringsort () ;} public static void Stringsort () {string[] str = new STRING[10]; Scanner in = new Scanner (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 a substring appears in a string

IndexOf's application, record the current subscript a. And then from the A + substring length continued after indexof, using the loop to achieve this function

Import java.util.scanner;/* * Number of occurrences of a substring 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: The maximum number of identical strings for a two string

Import java.util.scanner;/* * The longest common substring in two strings */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 string, the maximum public length will not exceed the len//common substring may be present in the middle for (int i = 0; i < len; i++)//control maximum length {for (int j = 0, k = len-i; k <= Len; k++, J + +)//J control several satisfies, K control substring the last element subscript does not cross the bounds {String sub = str2.substring (j, k); if (Str1.contains (sub)) return sub;}} return null;}}


Exercise 4: Remove both spaces (simulate trim method)

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


Java Learning Lesson 30th (often using object APIs)-String class: Class method Exercises

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.