Java string type in the detailed

Source: Internet
Author: User

character of the string
A: Once the string is assigned, it cannot be changed.
Note: This refers to the contents of the string cannot be changed, not the reference cannot be changed.

B: Literals As String objects and different objects created by constructing methods

string s = new string ("Hello") and string s = "Hello";
the former creates 2 objects, which create 1 objects. The string literal is an object, with new superfluous.

= =: Compare the reference type comparison is the address value is the same
equals: Comparing reference types By default is also the same as comparing address values, and the string class overrides the Equals () method to compare whether the content is the same

<span style= "FONT-SIZE:18PX;" >public class StringDemo2 {public static void main (string[] args) {string S1 = new String ("Hello"); String s2 = "Hello"; System.out.println (S1 = = s2);//FalseSystem.out.println (s1.equals (S2));//True}}</span>
/*
* See program Write results
* String If the variable is added, first open space, in stitching.
* String If the constant is added, is added first, then in the constant pool to find, if there is a direct return, otherwise, created.
*/
<span style= "FONT-SIZE:18PX;" >public class StringDemo4 {public static void main (string[] args) {String S1 = "Hello"; String s2 = "World"; String s3 = "HelloWorld"; System.out.println (S3 = = S1 + s2);//FalseSystem.out.println (S3.equals ((S1 + s2));//trueSystem.out.println (s3 = = " Hello "+" world ");//TrueSystem.out.println (s3.equals (" Hello "+" world ");//True}}</span>


/*
* The judging function of the string class:
* Boolean equals (Object obj): Compares the contents of strings for the same, case-sensitive
* Boolean equalsignorecase (String str): Compares the contents of a string for the same, ignoring case
* Boolean contains (String str): Determines whether a large string contains a small string
* Boolean StartsWith (String str): Determines whether a string begins with a specified string
* Boolean EndsWith (String str): Determines whether the string ends with a specified string
* Boolean isEmpty (): Determines whether the string is empty.
*
Note
* The string contents are empty and the string object is empty.
* String s = "";//There is an object, but the value is empty
* String s = null;//object does not exist, so method cannot be called
*/


/*
* Get function of String class
* int Length (): Gets the length of the string.
* Char charAt (int index): Gets the character at the specified index position
* int indexOf (int ch): Returns the index of the first occurrence of the specified character in this string.
* Why is this an int type instead of a char type?
* The reason is: ' A ' and 97 can actually represent ' a ', so the int type and char type are OK, more convenient
* 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 at the first occurrence of the specified character from the specified position in this string.
* int indexOf (string str,int fromIndex): Returns the index at the first occurrence of the specified string from the specified position in this string.
* String substring (int start): Intercepts the string starting at the specified position, from the default to the end.
* String substring (int start,int end): Intercepts the string from the specified position to the specified position.
*/

Requirements: Count the number of uppercase characters, lowercase alphabetic characters, number characters appearing in a string public class StringTest2 {public static void main (string[] args) {//define a string s = "Hello123world";//define three statistic variables int bigcount = 0;int Smallcount = 0;int Numbercount = 0;//iterates through the string to get each character. for (int x=0; x<s.length (); x + +) {Char ch = s.charat (x); <span style= "color: #ff0000;" >//determines whether the character belongs to that type of if (ch>= ' a ' && ch<= ' z ') {smallcount++;} else if (ch>= ' A ' && ch<= ' Z ') {bigcount++;} else if (ch>= ' 0 ' && ch<= ' 9 ') {numbercount++;} </span>}//output results. System.out.println ("Capital letter" +bigcount+ "one"); SYSTEM.OUT.PRINTLN ("Small Letter" +smallcount+ "one"); System.out.println ("number" +numbercount+ "a");}}

/*
* Conversion function of string:
* byte[] GetBytes (): Converts the string to a byte array.
* char[] ToCharArray (): Converts a string to a character array.
* Static string ValueOf (char[] CHS): Turns the character array into a string.
* Static string valueOf (int i): turns data of type int into a string.
*Note: The valueof method of the string class can turn any type of data into a string.
* String toLowerCase (): Turns the string into lowercase.
* String toUpperCase (): Turns the string into uppercase.
* String concat (String str): Concatenation of strings, convenient with + sign
*/

Requirement: The first letter of a string is capitalized and the remainder is lowercase. (only English uppercase and lowercase characters are considered) String str2 = "Listentomusic"; System.out.println (str2.substring (0, 1). toUpperCase () +str2.substring (1). toLowerCase ());

/*
* Other features of the String class:
*
* Replacement function:
* String replace (char Old,char new)
* String Replace (string old,string new)
*
* Remove string two spaces
* String Trim ()
*
* Compare two strings in a dictionary order
* int CompareTo (String str)
* int comparetoignorecase (String str)
A.compareto (b); return 0 a==b; return value >0 a>b, return value <0 a<b
*/

/* * Count the number of occurrences of a large String * Example: * In the string "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun" * Result: * Java has appeared 5 times * * Analysis: * Premise: is already aware of the large strings and small strings. * * A: Define a statistical variable, the initialization value is 0 * B: First in a large string to find the first occurrence of a small string position * A: The index is-1, the description does not exist, the return of the statistical variable * B: Index is not-1, description exists, statistical variable + + * C: The length of the previous index + string as the beginning A large string of times, returns a new string, and assigns the value of the string to a large string * D: Back to B */public class StringTest4 {public static void main (string[] args) {//define large string m axstring = "Woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";//define small string minString = "Java" ;//write function to implement int count = GetCount (maxstring, minstring); System.out.println ("Java appears in a large string:" + Count + "Times");} /* Two explicit: return value type: int parameter list: two strings */public static int GetCount (string maxstring, String minstring) {///define a statistic variable, initialize value is 0int Co UNT = 0;//First finds the first occurrence of a small string in a large string int index = Maxstring.indexof (minstring);//index is not-1, indicating existence, statistic variable ++while (Index! =-1) {count++; Take the length of the index + string as the starting position to intercept the last large string, return a new string, and re-assign the value of the string to the large string int startIndex = index + minstring.length (); maxstring = Maxstring.substring (StartIndex);//ContinueCheck index = Maxstring.indexof (minstring);} return count;}}





Java string type in the detailed

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.