1、2021/7/1312021/7/132The The StringString Class Class2021/7/133Constructing StringsConstructing Strings2021/7/134String pool(字符串池字符串池):JVM has a String pool,it saves a lot of String objects that created by shorthand initializer.Data sharingStack(栈)(栈):Store primitive data(char、byte、short、int、long、flo
2、at、double、boolean)and constructor.Data sharing.heap(堆)(堆):Store Object.2021/7/135Difference between new and shorthand initializerDifference between new and shorthand initializerQuestion:How many String objects are created?TwoConstructor:public String(String original)/other code.vNote:The parameter i
3、s a String object2021/7/136Difference between new and shorthand initializerDifference between new and shorthand initializerQuestion:How many String objects are created?One2021/7/1372021/7/138Strings Are ImmutableStrings Are Immutable2021/7/139Trace CodeTrace CodeAfter executing s=“java”;After execut
4、ing s=“HTML”;2021/7/1310Canonical StringsCanonical Strings(规范字符串)(规范字符串)improve efficiency and save memoryvUsing String objects intern method to return a canonical string2021/7/1311 intern:public native String intern()Returns a canonical representation for the string object.If s and t are strings su
5、ch that s.equals(t),it is guaranteed that s.intern()=t.intern().Canonical StringsCanonical Strings2021/7/1312ExamplesExamples2021/7/1313Finding String LengthFinding String Length2021/7/1314Retrieving Individual Characters in a StringRetrieving Individual Characters in a String W e l c o m e t o J a
6、v a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 message Indices message.charAt(0)message.charAt(14)message.length()is 15 2021/7/1315String Concatenation2021/7/1316String ConcatenationString Concatenation2021/7/1317Extracting Substrings2021/7/1318Extracting SubstringsExtracting Substrings W e l c o m e t o J
7、a v a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 message Indices message.substring(0,11)message.substring(11)2021/7/1319String ComparisonsString Comparisons2021/7/1320String Comparisons,cont.String Comparisons,cont.2021/7/1321String ConversionsString Conversionsv 2021/7/1322Finding a Character or a Substrin
8、g in a StringFinding a Character or a Substring in a String2021/7/1323Convert Character and Numbers to StringsConvert Character and Numbers to Strings2021/7/1324The Character ClassThe Character Class Character +Character(value:char)+charValue():char+compareTo(anotherCharacter:Character):int+equals(a
9、notherCharacter:Character):boolean+isDigit(ch:char):boolean +isLetter(ch:char):boolean +isLetterOrDigit(ch:char):boolean +isLowerCase(ch:char):boolean +isUpperCase(ch:char):boolean +toLowerCase(ch:char):char +toUpperCase(ch:char):char Constructs a character object with char value Returns the char va
10、lue from this object Compares this character with another Returns true if this character equals to another Returns true if the specified character is a digit Returns true if the specified character is a letter Returns true if the character is a letter or a digit Returns true if the character is a lo
11、wercase letter Returns true if the character is an uppercase letter Returns the lowercase of the specified character Returns the uppercase of the specified character 2021/7/1325ExamplesExamples2021/7/1326The StringBuffer ClassThe StringBuffer Class2021/7/1327 StringBuffer +StringBuffer()+StringBuffe
12、r(capacity:int)+StringBuffer(str:String)+append(data:char):StringBuffer+append(data:char,offset:int,len:int):StringBuffer+append(v:aPrimitiveType):StringBuffer+append(str:String):StringBuffer+capacity():int+charAt(index:int):char+delete(startIndex:int,endIndex:int):StringBuffer+deleteCharAt(int inde
13、x):StringBuffer+insert(index:int,data:char,offset:int,len:int):StringBuffer+insert(offset:int,data:char):StringBuffer+insert(offset:int,b:aPrimitiveType):StringBuffer+insert(offset:int,str:String):StringBuffer+length():int+replace(int startIndex,int endIndex,String str):StringBuffer+reverse():String
14、Buffer+setCharAt(index:int,ch:char):void+setLength(newLength:int):void+substring(startIndex:int):String+substring(startIndex:int,endIndex:int):String Constructs an empty string buffer with capacity 16 Constructs a string buffer with the specified capacity Constructs a string buffer with the specifie
15、d string Appends a char array into this string buffer Appends a subarray in data into this string buffer Appends a primitive type value as string to this buffer Appends a string to this string buffer Returns the capacity of this string buffer Returns the character at the specified index Deletes char
16、acters from startIndex to endIndex Deletes a character at the specified index Inserts a subarray of the data in the array to the buffer at the specified index Inserts data to this buffer at the position offset Inserts a value converted to string into this buffer Inserts a string into this buffer at
17、the position offset Returns the number of characters in this buffer Replaces the characters in this buffer from startIndex to endIndex with the specified string Reveres the characters in the buffer Sets a new character at the specified index in this buffer Sets a new length in this buffer Returns a
18、substring starting at startIndex Returns a substring from startIndex to endIndex 2021/7/1328StringBuffer ConstructorsStringBuffer Constructors2021/7/1329Appending New Contents into a String BufferAppending New Contents into a String Buffer2021/7/13302021/7/13312021/7/1332The StringTokenizer ClassThe
19、 StringTokenizer Class java.util.StringTokenizer +StringTokenizer(s:String)+StringTokenizer(s:String,delimiters:String)+StringTokenizer(s:String,delimiters:String,returnDelimiters:boolean)+countTokens():int+hasMoreTokens():boolean+nextToken():String +nextToken(delimiters:String):String Constructs a
20、string tokenizer for the string.Constructs a string tokenizer for the string with the specified delimiters.Constructs a string tokenizer for the string with the delimiters and returnDelims.Returns the number of remaining tokens.Returns true if there are more tokens left.Returns the next token.Return
21、s the next token using new delimiters.2021/7/1333Examples 1Examples 1String s=Java is cool.;StringTokenizer tokenizer=new StringTokenizer(s);System.out.println(The total number of tokens is +tokenizer.countTokens();while(tokenizer.hasMoreTokens()System.out.println(tokenizer.nextToken();.2021/7/1334E
22、xamples 2Examples 2String s=Java is cool.;StringTokenizer tokenizer=new StringTokenizer(s,ac);System.out.println(The total number of tokens is +tokenizer.countTokens();while(tokenizer.hasMoreTokens()System.out.println(tokenizer.nextToken();2021/7/1335Examples 3Examples 3String s=Java is cool.;String
23、Tokenizer tokenizer=new StringTokenizer(s,ac,true);System.out.println(The total number of tokens is +tokenizer.countTokens();while(tokenizer.hasMoreTokens()System.out.println(tokenizer.nextToken();2021/7/1336No no-arg Constructor in StringTokenizerNo no-arg Constructor in StringTokenizer2021/7/1337T
24、he Scanner ClassThe Scanner ClassString s=Welcome to Java!Java is fun!Java is cool!;Scanner scanner=new Scanner(s);scanner.useDelimiter(Java);while(scanner.hasNext()System.out.println(scanner.next();Welcome to!is fun!is cool!2021/7/1338Scanning Primitive Type ValuesScanning Primitive Type ValuesStri
25、ng s=1 2 3 4;Scanner scanner=new Scanner(s);int sum=0;while(scanner.hasNext()sum+=scanner.nextInt();System.out.println(Sum is +sum);2021/7/1339Console Input Using ScannerConsole Input Using ScannerSystem.out.print(Please enter an int value:);Scanner scanner=new Scanner(System.in);int i=scanner.nextInt();2021/7/1340Command-Line ParametersCommand-Line Parameters2021/7/1341Processing Command-Line ParametersProcessing Command-Line Parameters2021/7/1342问题?问题?