Java string Common methods < two >

Source: Internet
Author: User

1.Get Method

int length () Gets the length of the string,

char charAt (int index) gets the character at a specific position ( the corner label is out of bounds ),

int indexOf (String str) gets the position of a specific character (overload), finds the index value of the first occurrence of the substring, and returns 1 if it is not found.

int lastIndexOf (int ch) Gets the position of the last character, finds the last index value of the substring, and returns 1 if not found.

 Public class Demo02 {        publicstaticvoid  main (string[] args) {            String str= " ABC China AB China ";            System.out.println ("Number of characters:" +str.length ());            System.out.println ("gets the corresponding character according to the index value:" +str.charat (1));            System.out.println ("Find the index value of the first occurrence of a substring:" +str.indexof ("AB"));            System.out.println ("Find the last index value of the substring:" +str.lastindexof ("China"));}        }

Operation Result:

Number of characters: 9
Gets the corresponding character according to the index value: b
Find the index value of the first occurrence of a substring: 0
Find the last index value of a substring: 7

2. Judgment method

Whether Boolean endsWith (String str) ends with the specified character

The Boolean isEmpty () is 0 Long if:

Whether Boolean contains (charsequences) contains the specified sequence of applications: Search

Boolean equals (Object anobject) is equal

Boolean equalsignorecase (String anotherstring) ignores case equality

 Public classDemo03 { Public Static voidMain (string[] args) {String str= "Demo.java"; System.out.println ("Whether to start with the specified string:" +str.startswith ("De")); System.out.println ("Whether to end with the specified string:" +str.endswith ("Java")); System.out.println ("Determines whether the string content is empty:" +str.isempty ()); System.out.println ("Determines whether a string contains the specified content:" +str.contains ("Emo")); System.out.println ("Determine if the contents of 2 strings are consistent:" + "Demo.java". Equals (str)); System.out.println ("Determines whether the contents of 2 strings are consistent (ignoring case comparisons):" + "Demo.java". Equalsignorecase (str)); }}

Execution Result:

Whether to start with the specified string: True
Whether to end with the specified string: True
Determines whether the string contents are empty: false
Determines whether the string contains the specified content: true
Determine if the contents of 2 strings are consistent: false
Determines whether the contents of 2 strings are consistent (ignoring case comparisons): True

Java string Common methods < two >

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.