Java String Class-string common operations

Source: Internet
Author: User

/*
Common Operations for string:
String str= "ABCHGTHUJIDFG";
1, get
1.1 The string contains the number of characters, which is the length of the string.
int Lenrth ():
Str.length (); Note that the method of getting the length of the string differs from the method of getting the length of the array. Array in format: arr.length
1.2 Gets the character of the position based on the position.
char charAt (int index):
Str.charat (4);
1.3 Gets the position of the character based on the character.
int indexOf (int ch):
Str.indexof (' h ');///Note that the parameter is of type int because ASCII encoding is used, so the character arguments can be passed directly when using the function.
Str.indexof (' a '); Returns the position of a in the string from left to right.
int indexOf (int ch,int fromindex):
Str.indexof (' t ', 2); Returns the position at which the CH appears in the string, starting at the specified position in Fromindex.

int indexOf (String substr):
Str.indexof ("abc");//Returns the position of the substring in the first occurrence of the string.   That is the position of a. Returns 1 if it does not exist
int indexOf (String substr,int fromindex):
Str.indexof ("abc", 5);//starting at index position 5, gets the position of ABC in the string.

int lastIndexOf (int ch)://Returns the position of the last occurrence of a character.

2, judging
2.1 Whether a substring is contained in a string.
Boolean contains (str);
2.2 Whether the string has content.
Boolean isEmpty (); The principle is to determine whether the length is zero.
2.3 Whether the string starts with the specified content.
Boolean startsWith (String str);
2.4 Whether the string ends with the specified content.
Boolean endsWith (String str);
2.5 Determines whether the contents of the string are identical, and copies the Equals method in the object class.
Boolean equals (str);
2.6 Determine if the content is the same and ignore the case.
Boolean equalsignorecase ();

3, conversion
3.1 Turning character arrays into strings
Constructor: String (char[])
String (Char[],offset,count)//starts with a number of four characters indexed as offset.
static method: Static String copyvalueof (char[])
Static String copyvalueof (char[] data,int offset,int count)
3.2 Convert a string to a character array
Char[] ToCharArray ();
3.3 Convert a byte array to a string
Constructor: String (byte[])
String (Byte[],offset,count)//starts with a number of four characters indexed as offset.

3.4 Turning a string into a byte array
Byte[] GetBytes ();

3.5 Convert the base data type to a string:
static String valueOf (int)
Static String valueOf (double)


Special: Strings and byte arrays can be specified in the encoding table during the conversion process.
4, replace
String Replace (Oldchar,newchar)
5, cutting
    string[] Split (regex)   //Note back
Class Stringdemo {public static void main (string[] args) {method_3 ();} public static void Method1 () {string str= "ASBHDJUILDKJUISDF"; SOP ("String length:" +str.length ()); SOP ("character subscript 3:" +str.charat (3 ), SOP ("H first occurrence position subscript:" +str.indexof (' h ')), SOP ("Subscript position is 5" after D's position subscripts: "+str.indexof (' d ', 5)"), SOP ("substring Jui location:" + Str.indexof ("Jui")), SOP ("The last occurrence of D's position subscript:" +str.lastindexof ("D")), SOP ("shows the index of the substring appearing at the far right of the string:" +str.lastindexof ("Jui ", 8)); System.out.println ("Hello world!");} public static void Method2 () {char[] arr={' t ', ' r ', ' E ', ' Q ', ' A ', ' s ', ' d ', ' f '}; String S=new string (arr), SOP ("s=" +s), SOP ("-------------------"); String S1=new string (arr,3,4),//The number of characters starting at index 3, and 4 characters to form a string. SOP ("s1=" +s1); SOP ("-------------------"); String s2= "Hello World"; char[] Chs=s2.tochararray (), for (int i=0;i<chs.length; i++) {SOP ("chs[" +i+ "]=" +chs[i]);} SOP ("-------------------"); String s3=s2.replace (' H ', ' A ');//strong H is replaced by Asop (S3); String s4=s3.replace ("Aello", "Hello,,,,");//replace Aello with Hello,,,, SOP (S4);} public static void Sop (Object obj) {System.out.println (obj);} Public STAtic void Method_split () {String str= "Zhang San & John Doe"; String[] Arr=str.split ("&"); for (int i=0;i<arr.length; i++) {SOP (arr[i]);}} public static void Method_value () {int[] arr={1,2,3,4,5,6,7,8,9};for (int i=0;i<arr.length; i++) {SOP (arr[i]);}}    public static void Method_3 () {String str= ' Hello world! SOP (Str.touppercase ()), SOP (Str.tolowercase ()), SOP (Str.trim ()); String str1= "Abcdfa"; String str2= "ABCDF"; Sop (Str1.touppercase (). CompareTo (Str2.touppercase ()));}}

Is the type of the string array.
Set type of array: int[] arr ={1,2,3,4,5,6,7}
Double[] Arr ={1.0,1.2,1.3,1.4,1.5,1.5,1.7}
Byte[] Arr
char[] Arr ={' s ', ' d ', ' f ', ' g ', ' H '}
String[] arr ={"SDF", "FFF", "GGG", "Tgh", "GBF"}

6, SUBSTRING, getting part of a string
string substring (begin);//start from the specified position to the end, and if the corner label does not exist, an exception will occur
String substring (begin,end);//contains header does not contain tail

7, convert, remove spaces, compare
7.1 Convert a string to uppercase or lowercase
String toUpperCase ();
String toLowerCase ();
7.2 Remove multiple spaces at both ends of a string
String trim ();
7.3 Comparison of two strings in natural order
int CompareTo (string);
*/

Java String Class-string common operations

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.