016.2 String, 016.2 string

Source: Internet
Author: User

016.2 String, 016.2 string

 

#######################################
Comparison Method: equals ()
String length: int length ()
Character location: int indexOf (ch, fromIndex );
Returns the character char charAt (int) at the specified position)
Obtain some strings: String substring (int start, int end );
String to array: toCharArray ()

Search for exercises using the String method:
* 1. Whether the string starts with a specified string. The end is the same.
* Boolean startsWith (string)
* Boolean endsWith (string)
*
* 2. Whether the string contains another string.
* Boolean contains (string );
* Int indexOf (string) // If-1 is returned, it indicates that it does not exist.
*
* 3. the position where another string appears.
* Int indexOf (string)
* 4. Replace the specified string with another string.
* String replace (oldstring, newstring)
*
* 5. How to compare the size of a string?
*
* 6. Convert the string into a character array. Or a byte array.
* ToCharArray ()
* GetBytes ()
* 7. convert a letter string to an uppercase letter string.
* ToUpperCase ()
* ToLowerCase ();
* 8. Divide the string into multiple strings according to the specified method. "lisi, wangwu, zhaoliu" gets three names.
* String [] split (string );

# CompareTo (object) object Comparison Method

########### Find the number of other strings in a string

1 public class StringTest2_2 {2 public static void main (String [] args) {3/* 4 * Case 2: 5 * "witcasteritcasttyuiitcastodfghjitcast" there are several itcast 6*7 * ideas: 8*1 is nothing more than searching for another string in one string. IndexOf. 9*2. How can I find the second string that appears for the first time? 10*3. You do not need to start from the beginning, as long as you start from the first position + the length of the string to be searched to find the next position that appears for the first time. 11*4. When the returned position is-1, the search is complete. 12 */13 String str = "witcasteritcasttyuiitcastodfghjitcast"; 14 String key = "itcast"; 15 16 int count = getKeyCount (str, key); 17 System. out. println ("count =" + count); 18/* 19 int x = str. indexOf (key, 0); // start from scratch. 20 System. out. println ("x =" + x); 21 22 int y = str. indexOf (key, x + key. length (); // start from the specified start position. 23 System. out. println ("y =" + y); 24 25 int z = str. indexOf (key, y + key. length (); // start from the specified start position. 26 System. out. println ("z =" + z); 27 28 int a = str. indexOf (key, z + key. length (); // start from the specified start position. 29 System. out. println ("a =" + a); 30 31 int B = str. indexOf (key, a + key. length (); // start from the specified start position. 32 System. out. println ("B =" + B); 33 */34} 35 36/** 37 * obtain the number of times keys appear in str. 38 */39 public static int getKeyCount (String str, String key) {40 41 // 1, defines the variable. Record the location of each key found. 42 int index = 0; 43 // 2. Define the variable and record the number of occurrences. 44 int count = 0; 45 46 // 3, define a loop. If the index is not at-1, continue searching. 47 while (index = str. indexOf (key, index ))! =-1) {48 49 // specify the start position of the next query for each loop. 50 index = index + key. length (); 51 52 // each query time, count auto-increment. 53 count ++; 54} 55 return count; 56} 57 58}
View Code

################# Another program: requires that the string be printed from the length to the short

Public class StringTest2_3 {public static void main (String [] args) {/** Case 3: "itcast_sh" requires that the String be printed from length to length. Itcast_sh itcast_s tcast_sh */String str = "itcast"; printStringByLength (str);} public static void printStringByLength (String str) {// 1. It is found that it is a for nested loop through analysis. For (int I = 0; I <str. length (); I ++) {for (int start = 0, end = str. length ()-I; end <= str. length (); start ++, end ++) {// truncates a string based on start and end. String temp = str. substring (start, end); System. out. println (temp );}}}}
View Code

 



 

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.