Method of obtaining Java-string Class (IndexOf (); substring (), etc.)

Source: Internet
Author: User

Package com.day7.one;

public class DemoString2 {

/**
* @param args
* Get function of String class
* 1.int length (); Get string Lengths
* 2.char charAt (int index); Gets the character at the specified index position
* 3.int indexOf (int ch); Returns the index at the first occurrence of the specified character in this string
* 4.int indexOf (string str); Returns the index at the first occurrence of the specified string in this string
* 5.int indexOf (int ch,int fromIndex); Returns the index at the first occurrence of the specified character from the specified position in this string
* 6.int indexOf (string str,int fromIndex); Returns the index at the first occurrence of the specified string from the specified position in this string
* 7.lastIndexOf
* 8.String substring (int start); Intercept string from specified position, default to end
* 9.String substring (int start,int end); Intercept string from the specified position to the specified position
*/
public static void Main (string[] args) {
String s1= "Kobebryante";
System.out.println (S1.length ());
String s2= "Kobe Bryant";
System.out.println (S2.length ());//1. Get the length of a string

Char C1=s1.charat (0);
SYSTEM.OUT.PRINTLN (C1);//2. Output k

int Index=s1.indexof (' K ');
SYSTEM.OUT.PRINTLN (index);//3. Output 0
System.out.println (S1.indexof (' z '));//If not present, return-1

int Index1=s1.indexof ("be");
System.out.println (index1);//4. Gets the position of the first character in the "be" string
System.out.println (S1.indexof ("or"));//Return-1

int Index2=s1.indexof (' e ', 4);
System.out.println (INDEX2)//5. Look back from the specified position, output 10, and look back from the 4th one.

int Index3=s1.indexof ("NT", 4);
System.out.println (INDEX3);//6. Output 8

int Index4=s1.lastindexof (' e ');
System.out.println (index4);//7. Output 10, looking forward from the back, the first occurrence of the character
System.out.println (S1.indexof (' e '));//Output 3

String str1=s1.substring (4);
System.out.println (str1);//8. including the 4 position

String str2=s1.substring (4,6);
System.out.println (str2);//9. Output BR, including 4 not including 6 position; [4,6]

Trap questions
String s= "Kobe";
S.substring (2);
System.out.println (s);//print kobe,s unchanged, if System.out.println (s.substring (2));
System.out.println (s.substring (2));
}

}

Method of obtaining Java-string Class (IndexOf (); substring (), etc.)

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.