Black Horse programmer--string Class Summary

Source: Internet
Author: User
Tags truncated

-------<a href= "http://www.itheima.com" target= "blank" >android training </a> <a href= "/http/ Www.itheima.com "target=" blank ">java training </a>, look forward to communicating with you! ----------

String Class Summary

Methods of the String class

First, the construction method of the string class ()

string s = new string ();

Creating a String object from a byte array

string s = new string (byte[] bys);

Creating a String object from a byte array common

string s = new string (byte[] bys,int index, int length)

Creating a String object from a portion of a byte array

string s = new string (char[] CHS)

Creating a String object from a character array common

string s = new string (char[] CHS, int index, int length);

Creating a String object from a part of a character array

string s = new string (string);

Creating a character string object by passing a string to a constructor method

String s = "HelloWorld"

Assigning values directly to a string object is commonly used

Ii. member methods of the String class (judging, acquiring, converting, others)

1. Judging function

Boolean equals (Object obj) determines whether the contents of the string are the same, case-sensitive

Boolean equalsignorecase (String str) determines whether the contents of the string are the same, ignoring case

Boolean contains (string str) determines whether the string object contains the given string

Boolean startsWith (String str) determines whether the string object starts with the given string

Boolean endsWith (String str) determines whether the string object ends with the given string

Boolean isEmpty () to determine if the string object is empty, note that the data


2. Get the function

int length () gets the string length

char charAt (int index) returns the character at the given index in the string

int indexOf (int ch) returns the index of the first occurrence of the specified character in this string

int indexOf (String str) returns the index of the first occurrence of the specified string in this string

int indexOf (int ch,int fromIndex)

Returns the index of the first occurrence of the specified character in this string, starting at the specified position

int indexOf (String str,int fromIndex)

Returns the index of the first occurrence of the specified character in this string, opened from the specified position

string substring (int start) intercepts the string, returning the string that was truncated at the beginning of the specified position, with the same length of the original string

String substring (int start,int end)

Intercepts the string, returning from the specified position to the end of the specified position, the length of the original string of the truncated string unchanged

If the length of the string is start=0,end=, the original object is returned, otherwise the new object is returned


3. Conversion function

Byte[] GetBytes () converts a string to a byte array

Char[] ToCharArray () converts a string to a character array

static string copyvalueof (char[] CHS) converts a character array to a string

Underlying call to new String (char[] CHS)

static string ValueOf (char[] CHS) converts a character array to a string

Underlying call to new String (char[] CHS)

static string valueOf (any type variable name) converts any type to a string

String toLowerCase () turns the character into a lowercase original string unchanged

String toUpperCase () character programming uppercase the original string unchanged

String concat (String str) string link Original string unchanged

4. Other functions

String replace (char Oldchar,char Newchar) Replacement function The original string does not change

String Replace (string oldstring,string newstring) replaces function original string unchanged

String[] Split (string str) cutting function string in the split method of the original string invariant string

String trim () strips the string at both ends of the original string length unchanged

int CompareTo (String str) dictionary order comparison function

int comparetoignorecase (String str) dictionary order comparison function

Take the example of "count uppercase in a string, lowercase, number of occurrences of numeric characters" as a demonstration of some of the methods shown below:

Black Horse programmer--string Class Summary

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.