Common string APIs in Java

Source: Internet
Author: User

Common string APIs in Java


These APIs are encapsulated in a

Import Java.lang; (default import does not need to be imported manually)


String. Length ()

Returns the length of a string


String 1.equals (String 2)

Determine whether the contents of two strings are consistent ( cannot be used = =)


String 1.equalsIgnoreCase (String 2)

Determine whether the contents of two strings are consistent (case insensitive)


String. toLowerCase ()

Returns a lowercase string


String. toUpperCase ()

Return uppercase String


String 1.concat (String 2)

Return string 1 to String 2 (equivalent to "string 1" + "String 2")


String 1. CompareTo (String 2)

Returns the value of string 1 compared to string 2 (each of which is the same as ASCII) returns 0


/** Common Extraction method (if not found return-1) */public int indexOf (char ch)/** Returns the first occurrence of the character Ch's position */public int indexOf (string value)/** returns the first occurrence of the string The position of value */public int indexOf (char ch)/** returns the position of the last occurrence of CH, */public int indexOf (string value)/** returns the position of the last occurrence of the string value */ public string substring (int index)/** extracts the string portion starting from the position index */public string substring (int beginindex,int endindex)/** extract from the starting position of the cable String part */public string trim ()/** Returns a copy of the call string without any spaces before the end position index (excluding the end position) */

/** Common Splitting method */
The/** string class provides the split () method to split a string into a substring result as an array of strings returned */

public class Demo {public  static void Main (string[] args) {     String std = "A,b,c";     string[] Strarray = Str.split (",");     /** Sometimes the split delimiter will conflict with the keyword of the regular expression, then escape a bit, such as Str.split ("\\|"); Use | as delimiter *     /for (String item:strarray) System.out.println (item);//output a B c  }}


Common string APIs in Java

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.