Strings in Java

Source: Internet
Author: User

Api:java provides the application interface, burst out can be used directly.

String class: The string class represents strings, all string literals in Java programs (such as "abc" ) are implemented as instances of this class.

Because it is a constant class, a string cannot be changed if it is initialized. Each time a new string is created, the object is generated, even if two

The contents of the string are the same, and the two identical strings use ' = = ' to compare the result to false, because it is two different objects, and the Equals () method is used if you want to compare whether the content is the same.

Methods for creating a String object:

Stringdemo:

classStringdemo { Public Static voidMain (string[] args) {/*String S1 = "abc";//S1 is a class-type variable, and "ABC" is an object.        The greatest feature of a string: once initialized, it cannot be changed.        String s2 = new String ("abc");        What's the difference between S1 and S2?        S1 has an object in memory.                S2 has two objects in memory.        System.out.println (S1==S2);        System.out.println (s1.equals (S2)); The//string class copies the Equals method in the object class,//The method is used to determine whether the string is the same. */String S= "ABCDE"; }    /*The string class is a description of the strings.    This class defines a method that is specifically used to manipulate strings. "ABC":*/        }}
Stringdemo

Common Methods for strings:

1 the string class is useful for describing a string thing. 2 then it provides several ways to manipulate the string. 3 4 What are the common operations? 5"ABCD"6 71, get. 81.1The number of characters contained in the string, that is, the length of the string. 9         intLength (): Gets the length. Ten1.2gets the position of a character based on its location.  One         CharCharAt (intindex): A1.3gets the character's position in the string, based on the character.  -         intIndexOf (intCH): Returns the position of the first occurrence of CH in the string.  -         intIndexOf (intChintFromIndex): Gets the position where Ch appears in the string, starting at the specified position in FromIndex.  the  -         intindexOf (String str): Returns the position of the first occurrence of STR in a string.  -         intIndexOf (String str,intFromIndex): Gets the position where Str appears in the string, starting at the specified position in FromIndex.  -  +         intLastIndexOf (intch): -  +          A2, judged.  at2.1whether a string contains a substring.  -         Booleancontains (str): -Special: IndexOf (str): Can index str first occurrence position, if return-1indicates that the STR does not exist in the string.  - Therefore, it can also be used to determine whether the specified is included.  -                 if(Str.indexof ("AA")!=-1) -  in and the method can be judged, but also to obtain the location of the occurrence.  -  to2.2whether there is content in the character.  +         BooleanIsEmpty (): The principle is to determine whether the length is 0. -2.3whether the string starts with the specified content.  the         BooleanStartsWith (str); *2.4whether the string ends with the specified content.  $         BooleanEndsWith (str);Panax Notoginseng2.5determines whether the string contents are the same. The Equals method in the object class is replicated.  -         Booleanequals (str); the2.6determine if the content is the same and ignore the case.  +         Booleanequalsignorecase (); A      the3, convert.  +3.1converts a character array into a string.  -Constructor: String (Char[]) $String (Char[],offset,count): Converts a portion of a character array into a string.  $  - static method: -                 StaticString copyvalueof (Char[] ch); the                 StaticString copyvalueof (Char[] Data,intOffsetintcount) - Wuyi                 StaticString ValueOf (Char[]): the  -          Wu3.2 Converts a string into a character array. ** -         Char[] ToCharArray (): About  $3.3converts a byte array into a string.  -String (byte[]) -String (byte[],offset,count): Converts a portion of a byte array into a string.  -  A3.4converts a string into a byte array.  +             byte[] getBytes (): the3.5converts the base data type to a string.  -         StaticString ValueOf (int) $         StaticString ValueOf (Double) the  the         //" ";//string.valueof (3); the  the Special: Strings and byte arrays can be specified in the encoding table during the conversion process.  -  in4, replace the String replace (Oldchar,newchar); the  About5, Cutting the string[] Split (regex); the  the6, sub-string. Gets the part of the string.  + String substring (begin); - String substring (begin,end); the Bayi7, convert, remove spaces, compare.  the7.1turns a string into uppercase or lowercase.  the String touppercase (); - String toLowerCase (); -  the7.2removes multiple spaces at both ends of a string.  the String trim (); the  the7.3compares two strings in a natural order.  -         intCompareTo (string);
Common Java Methods

Strings 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.