Kĩ thuật lập trình - Chương 30: Strings, characters and regular expressions
Regular expression
Sequence of characters and symbols
Useful for validating input and ensuring data format
E.g., ZIP code
Facilitate the construction of a compiler
Regular-expression operations in String
Method matches
Matches the contents of a String to regular expression
Returns a boolean indicating whether the match succeeded
83 trang |
Chia sẻ: huyhoang44 | Lượt xem: 790 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Kĩ thuật lập trình - Chương 30: Strings, characters and regular expressions, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
30Strings, Characters and Regular Expressions 1The chief defect of Henry KingWas chewing little bits of string.—Hilaire BellocVigorous writing is concise. A sentence should contain no unnecessary words, a paragraph no unnecessary sentences.—William Strunk, Jr.I have made this letter longer than usual, because I lack the time to make it short.—Blaise Pascal2OBJECTIVESIn this chapter you will learn: To create and manipulate immutable character string objects of class String.To create and manipulates mutable character string objects of class StringBuilder.To create and manipulate objects of class Character.To use a StringTokenizer object to break a String object into tokens.To use regular expressions to validate String data entered into an application. 330.1 Introduction 30.2 Fundamentals of Characters and Strings 30.3 Class String 30.3.1 String Constructors 30.3.2 String Methods length, charAt and getChars 30.3.3 Comparing Strings 30.3.4 Locating Characters and Substrings in Strings30.3.5 Extracting Substrings from Strings 30.3.6 Concatenating Strings 30.3.7 Miscellaneous String Methods 30.3.8 String Method valueOf 30.4 Class StringBuilder 30.4.1 StringBuilder Constructors 30.4.2 StringBuilder Methods length, capacity, setLength and ensureCapacity 30.4.3 StringBuilder Methods charAt, setCharAt, getChars and reverse 30.4.4 StringBuilder append Methods 30.4.5 StringBuilder Insertion and Deletion Methods 30.5 Class Character30.6 Class StringTokenizer30.7 Regular Expressions, Class Pattern and Class Matcher30.8 Wrap-Up 430.1 IntroductionString and character processingClass java.lang.StringClass java.lang.StringBuilderClass java.lang.CharacterClass java.util.StringTokenizerRegular expressionsValid inputPackage java.util.regexClasses Matcher and Pattern530.2 Fundamentals of Characters and StringsCharacters“Building blocks” of Java source programsCharacter literalsUnicode character setStringSeries of characters treated as single unitMay include letters, digits, special characters.Object of class StringString literals6Performance Tip 30.11Java treats all string literals with the same contents as a single String object that has many references to it. This conserves memory. 730.3 Class StringClass StringRepresent strings in Java830.3.1 String ConstructorsFig. 30.1 demonstrates four constructorsNo-argument constructorOne-argument constructor A String objectOne-argument constructor A char arrayThree-argument constructorA char arrayAn integer specifies the starting positionAn integer specifies the number of characters to access9Software Engineering Obervation 30.1It is not necessary to copy an existing String object. String objects are immutable—their character contents cannot be changed after they are created, because class String does not provide any methods that allow the contents of a String object to be modified. 10OutlineStringConstructors.java Line 12Line 13Line 14Line 15Program outputNo-argument constructor creates an empty stringOne-argument constructor creates a String that contains the same sequence of characters as the String argumentOne-argument constructor creates a String that contains a copy of the characters in the array argumentThree-argument constructor creates a String that contains a copy of partial characters in the array argument11Common Programming Error 30.1Attempting to access a character that is outside the bounds of a string (i.e., an index less than 0 or an index greater than or equal to the string’s length) results in a StringIndexOutOfBoundsException. 1230.3.2 String Methods length, charAt and getCharsMethod lengthDetermine the length of a StringLike arrays, Strings always “know” their sizeUnlike array, Strings do not have length instance variableMethod charAtGet character at specific location in String Method getCharsGet entire set of characters in String 13OutlineStringMiscellaneous.java (1 of 2)Line 15Lines 20-21Determine number of characters in String s1Display the characters of the string s1 in reverse order14OutlineStringMiscellaneous.java (2 of 2)Line 24Program outputCopy (some of) s1’s characters to charArray1530.3.3 Comparing StringsComparing String objectsMethod equalsMethod equalsIgnoreCaseMethod compareToMethod regionMatches16OutlineStringCompare.java (1 of 3)Line 17Line 23Method equals tests two objects for equality using lexicographical comparisonEquality operator (==) tests if both references refer to same object in memory17OutlineStringCompare.java (2 of 3)Line 29Lines 36-44Line 47Test two objects for equality, but ignore case of letters in StringsMethod compareTo compares String objectsMethod regionMatches compares portions of two String objects for equality18OutlineStringCompare.java (3 of 3)Program output19Common Programming Error 30.2Comparing references with == can lead to logic errors, because == compares the references to determine whether they refer to the same object, not whether two objects have the same contents. When two identical (but separate) objects are compared with ==, the result will be false. When comparing objects to determine whether they have the same contents, use method equals.20OutlineStringStartEnd.java (1 of 2)Lines 13 and 22Method startsWith determines if String starts with specified characters21OutlineStringStartEnd.java (2 of 2)Line 32Program outputMethod endsWith determines if String ends with specified characters2230.3.4 Locating Characters and Substrings in StringsSearch for characters in StringMethod indexOfMethod lastIndexOf23OutlineStringIndexMethods.java (1 of 3)Lines 12, 14 and 16Lines 20, 22 and 24Method indexOf finds first occurrence of character in StringMethod lastIndexOf finds last occurrence of character in String24OutlineStringIndexMethods.java (2 of 3)Lines 28, 30, 32, 36, 38 and 40Methods indexOf and lastIndexOf can also find occurrences of substrings25OutlineStringIndexMethods.java (3 of 3)Program output2630.3.5 Extracting Substrings from StringsCreate Strings from other StringsMethod substring27OutlineSubString.javaLine 12Line 15Program output Beginning at index 20, extract characters from String lettersExtract characters from index 3 to 6 from String letters2830.3.6 Concatenating StringsMethod concatConcatenate two String objects29OutlineStringConcatenation.java Line 13Line 14Program output Concatenate String s2 to String s1 However, String s1 is not modified by method concat3030.3.7 Miscellaneous String MethodsMiscellaneous String methodsReturn modified copies of StringReturn character array31OutlineStringMiscellaneous2.java (1 of 2)Line 16Line 19Line 20Line 23Use method replace to return s1 copy in which every occurrence of ‘l’ is replaced with ‘L’Use method toUpperCase to return s1 copy in which every character is uppercaseUse method toLowerCase to return s2 copy in which every character is uppercaseUse method trim to return s3 copy in which whitespace is eliminated32OutlineStringMiscellaneous2.java (2 of 2)Line 26Program outputUse method toCharArray to return character array of s13330.3.8 String Method valueOfString provides static class methodsMethod valueOfReturns String representation of object, data, etc.34OutlineStringValueOf.java (1 of 2)Line 12Line 13Use literal value 10000000000L as the initial values of long variableUse literal value 2.5f as the initial values of float variable35OutlineStringValueOf.java (2 of 2)Lines 18-29Program outputstatic method valueOf of class String returns String representation of various types3630.4 Class StringBuilderClass StringBuilderWhen String object is created, its contents cannot change Used for creating and manipulating dynamic string datai.e., modifiable StringsCan store characters based on capacityCapacity expands dynamically to handle additional charactersUses operators + and += for String concatenation37Performance Tip 30.2 Java can perform certain optimizations involving String objects (such as sharing one String object among multiple references) because it knows these objects will not change. Strings (not StringBuilders) should be used if the data will not change.38Performance Tip 30.3In programs that frequently perform string concatenation, or other string modifications, it is more efficient to implement the modifications with class StringBuilder (covered in Section 30.4).3930.4.1 StringBuilder ConstructorsFour StringBuilder constructorsNo-argument constructorCreates StringBuilder with no charactersCapacity of 16 charactersOne-argument constructorint argumentSpecifies the initial capacityOne-argument constructorString argumentCreates StringBuilder containing the characters in the String argument40OutlineStringBuilderConstructors.java Line 8Line 9Line 10Lines 12-14Program output No-argument constructor creates empty StringBuilder with capacity of 16 charactersOne-argument constructor creates empty StringBuilder with capacity of specified (10) charactersOne-argument constructor creates StringBuilder with String “hello” and capacity of 16 charactersMethod toString returns String representation of StringBuilder4130.4.2 StringBuilder Methods length, capacity, setLength and ensureCapacityMethod lengthReturn StringBuilder lengthMethod capacityReturn StringBuilder capacityMethod setLengthIncrease or decrease StringBuilder lengthMethod ensureCapacitySet StringBuilder capacityGuarantee that StringBuilder has minimum capacity42OutlineStringBuilderCapLen.java Line 11Line 11Line 13Line 16Program outputMethod length returns StringBuilder lengthMethod capacity returns StringBuilder capacityUse method ensureCapacity to set capacity to 75Use method setLength to set length to 10Only 10 characters from StringBuilder are printed43Performance Tip 30.4Dynamically increasing the capacity of a StringBuilder can take a relatively long time. Executing a large number of these operations can degrade the performance of an application. If a StringBuilder is going to increase greatly in size, possibly multiple times, setting its capacity high at the beginning will increase performance.4419.4.3 StringBuilder Methods charAt, setCharAt, getChars and reverseManipulating StringBuilder charactersMethod charAtReturn StringBuilder character at specified indexMethod setCharAtSet StringBuilder character at specified indexMethod getCharsReturn character array from StringBuilder Method reverseReverse StringBuilder contents45OutlineStringBuilderChars.java (1 of 2)Line 12Line 15Return StringBuilder characters at indices 0 and 4, respectivelyReturn character array from StringBuilder46OutlineStringBuilderChars.java (2 of 2)Lines 21 and 22Line 25Program outputReplace characters at indices 0 and 6 with ‘H’ and ‘T,’ respectivelyReverse characters in StringBuilder47Common Programming Error 30.3Attempting to access a character that is outside the bounds of a StringBuilder (i.e., with an index less than 0 or greater than or equal to the StringBuilder’s length) results in a StringIndexOutOfBoundsException.4830.4.4 StringBuilder append MethodsMethod appendAllow data values to be added to StringBuilder49OutlineStringBuilderAppend.java (1 of 3)50OutlineStringBuilderAppend.java (2 of 3)Line 21Line 23Line 25Line 27Lines 29, 31, 33, 35, 37 and 39Line 41Append String “hello” to StringBuilderAppend a new line character to StringBuilderAppend String “goodbye”Append “a b c d e f”Append “a b c”Append boolean, char, int, long, float and doubleAppend StringBuilder51OutlineStringBuilderAppend.java (3 of 3)Program output5230.4.5 StringBuilder Insertion and Deletion MethodsMethod insertAllow data-type values to be inserted into StringBuilder Methods delete and deleteCharAtAllow characters to be removed from StringBuilder53OutlineStringBuilderInsert.java (1 of 3)54OutlineStringBuilderInsert.java (2 of 3)Lines 20-38Use method insert to insert data in beginning of StringBuilder55OutlineStringBuilderInsert.java (3 of 3)Line 43Line 44Program outputUse method deleteCharAt to remove character from index 10 in StringBuilderRemove characters from indices 2 through 5 (inclusive)5630.5 Class CharacterTreat primitive variables as objectsType wrapper classesBooleanCharacterDoubleFloatByteShortIntegerLongWe examine class Character57OutlineStaticCharMethods.java (1 of 3)Line 12Obtain first character of the input string58OutlineStaticCharMethods.java (2 of 3)Line 15Line 16Line 18Line 20Line 21Line 23Lines 25 and 27Lines 29 and 31Determine whether c is defined Unicode character setDetermine whether c can be used as first character in identifierDetermine whether c is defined Unicode digitDetermine whether c can be used as identifier characterDetermine whether c is a letterDetermine whether c is letter or digitDetermine whether c is lowercase and uppercase, respectivelyConvert c to its uppercase and lowercase, respectively59OutlineStaticCharMethods.java (3 of 3)Program output60OutlineStaticCharMethods2.java (1 of 2)Line 28Use method forDigit to convert int digit to number-system character specified by int radix61OutlineStaticCharMethods2.java (2 of 2)Line 35Program outputUse method digit to convert char c to number-system integer specified by int radix62OutlineOtherCharMethods.java Lines 8-9Line 12 Line 12Line 14Program outputAssign two character literals to two Character objects. Auto-boxing occurs.Obtain the char value stored in Character object c1Method toString returns a string representation of the Character objectUse method equals to determine whether c1 has the same contents as c26330.6 Class StringTokenizerTokenizerPartition String into individual substringsUse delimiterTypically whitespace characters (space, tab, newline, etc)Java offers java.util.StringTokenizer64OutlineTokenTest.java Line 17Line 19Lines 21-22Program outputUse StringTokenizer to parse String using default delimiter “ \n\t\r”Count number of tokensDisplay next token as long as tokens exist6530.7 Regular Expressions, Class Pattern and Class MatcherRegular expressionSequence of characters and symbolsUseful for validating input and ensuring data formatE.g., ZIP codeFacilitate the construction of a compilerRegular-expression operations in StringMethod matchesMatches the contents of a String to regular expressionReturns a boolean indicating whether the match succeeded6630.7 Regular Expressions, Class Pattern and Class Matcher (Cont.)Predefine character classesEscape sequence that represents a group of characterDigitNumeric characterWord characterAny letter, digit, underscoreWhitespace characterSpace, tab, carriage return, newline, form feed67Fig. 30.19 | Predefined character classes. 6830.7 Regular Expressions, Class Pattern and Class Matcher (Cont.)Other patternsSquare brackets ([])Match characters that do not have a predefined character classE.g., [aeiou] matches a single character that is a vowelDash (-)Ranges of charactersE.g., [A-Z] matches a single uppercase letter^Not include the indicated charactersE.g., [^Z] matches any character other than Z6930.7 Regular Expressions, Class Pattern and Class Matcher (Cont.)QuantifiersPlus (+)Match one or more occurrencesE.g., A+Matches AAA but not empty stringAsterisk (*)Match zero or more occurrencesE.g., A*Matches both AAA and empty stringOthers in Fig. 30.2270Fig. 30.22 | Quantifiers used in regular expressions. 71OutlineValidateInput.java (1 of 2)Lines 9, 15, 22 and 28Method matches returns true if the String matches the regular expression72OutlineValidateInput.java (2 of 2)Method matches returns true if the String matches the regular expression73OutlineValidate.java (1 of 3)74OutlineValidate.java (2 of 3)75OutlineValidate.java (3 of 3)Program outputIndicate that the entry for “zip” was invalid7630.7 Regular Expressions, Class Pattern and Class Matcher (Cont.)Replacing substrings and splitting stringsString method replaceAllReplace text in a string with new textString method replaceFirstReplace the first occurrence of a pattern matchString method splitDivides string into several substrings77OutlineRegexSubstitution.java (1 of 2)Line 14Line 19Line 26Replace every instance of “*” in firstString with “^”Replace every instance of “stars” in firstString with “carets”Replace every word in firstString with “word”78OutlineRegexSubstitution.java (2 of 2)Line 32Line 38replaceFirst replaces a single occurrence of the regular expressionsplit returns array of substrings between matches of the regular expression7930.7 Regular Expressions, Class Pattern and Class Matcher (Cont.)Class PatternRepresents a regular expressionClass MatchContains a regular-expression pattern and a CharSequenceInterface CharSequenceAllows read access to a sequence of charactersString and StringBuilder implement CharSequence80Common Programming Error 30.4A regular expression can be tested against an object of any class that implements interface CharSequence, but the regular expression must be a String. Attempting to create a regular expression as a StringBuilder is an error.81OutlineRegexMatches.java (1 of 2)Lines 11-12Line 20Line 22Line 23Program outputcompile creates a Pattern object for regular expressionmatcher creates the Matcher object for the compiled regular expression and the matching sequencefind gets the first substring that matches the regular expressiongroup returns the string from the search object that matches the search pattern82Common Programming Error 30.5Method matches (from class String, Pattern or Matcher) will return true only if the entire search object matches the regular expression. Methods find and lookingAt (from class Matcher) will return true if a portion of the search object matches the regular expression.83
Các file đính kèm theo tài liệu này:
- javahtp7e_30_1447_2722.ppt