Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class More on StringsMore on Strings Strings reviewStrings review Character.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class More on StringsMore on Strings Strings reviewStrings review Character."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class More on StringsMore on Strings Strings reviewStrings review Character TestingCharacter Testing Matching/Finding SubstringsMatching/Finding Substrings Building SubstringsBuilding Substrings Modifying StringsModifying Strings Tokenizing StringsTokenizing Strings

2 March 2005 2R. Smith - University of St Thomas - Minnesota Strings review Are they primitives or objects?Are they primitives or objects? Find the nth character?Find the nth character? What does str1==str2 mean?What does str1==str2 mean? What about str1.startsWith(str2) ?What about str1.startsWith(str2) ?

3 March 2005 3R. Smith - University of St Thomas - Minnesota Character Testing Character.isDigit(ch)Character.isDigit(ch) Character.isLetter(ch)Character.isLetter(ch) Character.isLetterOrDigit(ch)Character.isLetterOrDigit(ch) Character.isLowerCase(ch)Character.isLowerCase(ch) Character.isUpperCase(ch)Character.isUpperCase(ch) Character.isSpaceChar(ch)Character.isSpaceChar(ch) Character.isWhiteSpace(ch)Character.isWhiteSpace(ch) –Means a space, tab, or newline

4 March 2005 4R. Smith - University of St Thomas - Minnesota Matching Substrings Str1.startsWith(str2)Str1.startsWith(str2) Str1.endsWith(str2)Str1.endsWith(str2) Str1.regionMatches(o1, “Str2”, o2, len)Str1.regionMatches(o1, “Str2”, o2, len) –O1 = starting point in Str1 –Str2 = string to match with –O2 = starting point in Str2 –Len = length to match

5 March 2005 5R. Smith - University of St Thomas - Minnesota Making Substrings Str.substring(i)Str.substring(i) –Returns the part of Str beginning at ‘i’ Str.substring(i,j)Str.substring(i,j) –Returns the part from ‘i’ to ‘j’ –‘j’th character not included What can we use this for?What can we use this for?

6 March 2005 6R. Smith - University of St Thomas - Minnesota Modifying Strings –We can’t really – we just make new ones Str1.concat(Str2) – combines themStr1.concat(Str2) – combines them Str1.replace(‘x’, ‘y’)Str1.replace(‘x’, ‘y’) –Replace every ‘x’ with a ‘y’ Str1.trim()Str1.trim() –Removes all leading and trailing white space –A Java lesson – there’s always the ‘right’ method somewhere

7 March 2005 7R. Smith - University of St Thomas - Minnesota Tokenizing Strings A way of pulling the pieces out of a stringA way of pulling the pieces out of a string Import java.util.StringTokenizer;Import java.util.StringTokenizer; Constructors:Constructors: –St1 = new StringTokenizer(string); –St2 = new StringTokenizer(string,”-”); –St3 = new StringTokenizer(string,”-”, true);

8 March 2005 8R. Smith - University of St Thomas - Minnesota Using the Tokenizer St1.countTokens()St1.countTokens() –Returns number of tokens in the string St1.hasMoreTokens()St1.hasMoreTokens() –Returns ‘true’ if more tokens there Str1.nextToken()Str1.nextToken() –Returns next token as a string

9 March 2005 9R. Smith - University of St Thomas - Minnesota Cheap Tokenizing The ‘split’ methodThe ‘split’ method –String tokens[] = Str1.split(“ “); –Returns array of strings delimited by blanks It’s hard to ask for more than blanksIt’s hard to ask for more than blanks –Requires a ‘regular expression’ –Example: “[@.]” looks for @ or.

10 March 2005 10R. Smith - University of St Thomas - Minnesota Other things in Chapter 10 Converting strings into char arraysConverting strings into char arrays Numbers to string representationsNumbers to string representations “Boxing” and “Unboxing”“Boxing” and “Unboxing” –Has to do with “object” versions of primitive types –There are ‘wrapper classes’ for primitive types Essentially ‘object’ versions of the typesEssentially ‘object’ versions of the types StringBuilderStringBuilder –A way of making strings you can modify –“Real” strings can’t be modified You can only build new ones based on old onesYou can only build new ones based on old ones

11 March 2005 11R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class More on StringsMore on Strings Strings reviewStrings review Character."

Similar presentations


Ads by Google