6.0 Strings String

Source: Internet
Author: User
Tags truncated

Outline:

6.1 Creating a String

Definition: The string class, which is not the basic data type of Java, is the type of strings, but can be used as a basic data type and declared in double quotation marks. A string variable is created in Java using the constructor type method of the string class. Common construction methods are as follows:

1.String ()

A string object that can represent a sequence of empty characters.

< example >:string s=new String ();

2. String (char a[])

Creates a string object with a character array A.

< example >:char a[]=new char{' i ', ' love ', ' You '};

String S=new string (a);

Equivalent to: String S=new string ("I love You");

3.String (char a[],int offset,int length)

Extracts a portion of the character array A to create a string object. The parameter offset indicates the position at which the string is to be truncated, and length indicates how long the string was truncated.

< example >:char a[]={' s ', ' t ', ' r ', ' I ', ' n ', ' G '};

String s=string (a,1,3);

Equivalent to: String s={"tri"};

6.2 String Manipulation

Definition: For a string that has already been declared, it can be manipulated accordingly, and the string class contains many methods.

6.2.1 String Connection

1. Multiple string connections

When connecting multiple strings, the "+" is concatenated between each of the two concatenated strings, and "+" is the string's connector, and the connection becomes a new string.

< example >:string s1=new String ("Hello");

String S2=new string ("word");

String s=s1+ "" +S2;

Run Result: Hello Word

2. Connecting other data types

When strings are connected to other data types, the "+" connector is used at the same time, and the return value after the connection is a string.

< example >:int booktime=4;

float computer=2.5f;

System.out.println ("I Spend" +booktime+ "hours a day reading, with" +computer+ "hours on the machine operation.");

6.2.2 Getting string information

1. Get the string length

Str.length (); method

STR: Refers to a String object

return value: string length of type int

String a= "Hello World";

System.out.println (a.length);//Output 11.

Space is also counted as length

2. Get the index position of the specified character

Str.indexof (substr);//returns the index where the character first appears

Str.lastindexof (substr);//returns the index where the last occurrence of the character

STR: Any String object

SUBSTR: Characters to search for

On the computer, the returned string object index starts at 0.

3. Get the character at the specified index position

Use the Charat () method of the string class to get the character under the index at the specified index, and to return the index of the character.

Format: Str.charat (int index);

STR: arbitrary string

Index: An integer value that specifies the subscript of the character to be returned.

Return value: Returns the character at the specified index position.

< example >:string s= "Hello World";

Char S1=s.charat (0);

System.out.println (S1);//output: H

6.2.3 Judgment string

Judging the string is primarily to determine whether the string is equal, and whether the string starts or ends with the specified string. This is about determining whether strings are equal.

(1) Equals () method

Strings are strictly case-sensitive when compared using the Equals method, which returns true if two strings are thrown with the same character and length, otherwise returns FOLSE.

Syntax: Str.equals (String otherstr)

STR: A string object that participates in the comparison

OTHERSTR: Another string object that participates in the comparison

Return value: Returns a Boolean type.

(2) Equalslgnorecase () method

When you use the Equalslgnorecase method to compare strings, ignore case-sensitive, and then this condition returns true if two strings are thrown with the same character and length, otherwise Folse is returned.

Syntax: Str.equalslgnorecase (String otherstr)

STR: A string object that participates in the comparison

OTHERSTR: Another string object that participates in the comparison

Return value: Returns a Boolean type.

In string comparisons, "= =" is not allowed, which is a serious error.

 Packagebasic data type; Public classString literal { Public Static voidMain (string[] args) {//String different definition methods have different meaningsString str= "string constant"; String str1=NewString ("string constant");//construction method,String str2=NewString ("string constant");//new open space for memoryString str3= "string constant";        System.out.println (str);        System.out.println (STR1);        System.out.println (STR2); System.out.println (str==STR1);//returns false because the definition differs in different waysSystem.out.println (STR==STR2);//returns false because the definition differs in different waysSystem.out.println (STR1==STR2);//returns false because the definition differs in different waysSystem.out.println (STR==STR3);//returns true because the definitions are the same and have the same meaning//the = = Operation actually compares the memory address is equal, the essence is not not the comparison value, does not apply when new//workaround. EqualsSystem.out.println (Str1.equals (str2));//true, which indicates whether the string is equalSystem.out.println (Str.equals (str1)); //String Information//. Length () string length, Parenthesized is the method, without parentheses is the propertySystem.out.println ("str String length =" +str.length ()); //Incoming Parameters        Char[]c=New Char[]{' Me ', ' very ', ' good '}; String STR4=NewString (c);        System.out.println (STR4); //. indexof the first position of a substring of a string from (to the rear), returning the index value of the first word found//there is more to return the first one. IndexOfSystem.out.println ("Position of the constant word =" +str.indexof ("constant"))); //returns the position of the first wordSystem.out.println ("Position of the constant word =" +str.indexof ("string constant")); //if the string is not found, the return-1 judgment contains a string, <0 No, >=0 containsSYSTEM.OUT.PRINTLN ("constant word position =" +str.indexof ("xxx"))); //. LastIndexOf retrieves the location from (back to front), but returns the index value still starting from 0System.out.println (Str.lastindexof ("regular")); //Get character        CharC1=str.charat (0);    SYSTEM.OUT.PRINTLN (C1); }} Answer: string constant string constant string constantfalsefalsefalsetruetruetruestr string length=5I'm a good place for regular characters.=3the position of the constant word=2the position of the constant word=-13Word

6.0 Strings String

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.