Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced String handling

Similar presentations


Presentation on theme: "Advanced String handling"— Presentation transcript:

1 Advanced String handling
Things we might want to do Finding patterns using regular expressions Manipulating Strings Splitting Processing Tokens Basic methods to review Substring(), charAt(), indexOf(), toLowerCase(), startsWith(), endsWith(), firstIndexOf(), lastIndexOf(), trim(), length()

2 Pattern matching Regular expressions is a syntax for pattern matching used by many programming languages Examples of regular expression syntax: [aceF] matches any of the letters enclosed in [ ] * matches zero or more occurrences of a pattern + matches one or more occurrences of a pattern \s matches whitespace String methods that use regular expressions include matches(), split(), replaceAll() More concrete examples are on the following slides Note: This page is a brief overview; regular expression syntax has much more in it

3 Manipulationg Strings
Problem: A String is an immutable object Bad solution (1453 milliseconds on my computer): Repeatedly create a new string from an old one String str; for (int i=0; i<10000; i++) str += “abcdef”; Better solution (0 milliseconds on my computer): Use StringBuilder, a mutable string class StringBuilder build = new StringBuilder(10000); For (int i=0; i<10000; i++) { build.append(“abcdef”); } String str = build.toString();

4 Splitting tokens Extracting information from a string
Output January pm Splitting tokens Extracting information from a string Example: String date = "January 23, :32:15 pm"; String[] data = date.split("[, :]+"); for (int i=0; i<data.length; i++) System.out.println(data[i]); The argument to split: determines how the date is converted to an array Characters enclosed between [ and ] are delimiters Space, comma and colon Split when one or more (+) delimiters are found in a row Definition: A token is a group of characters treated as a unit

5 Another way to split tokens
Output ( ( ) / * Another way to split tokens String Tokenizer class String expression = "(( )/ * "; // The next line removes white space expression = expression.replaceAll("\\s+",""); StringTokenizer tokenizer = new StringTokenizer(expression, "()+-/*", true); while (tokenizer.hasMoreTokens()) { System.out.println(tokenizer.nextToken()); } Definition: white space includes space, tab, new line characters


Download ppt "Advanced String handling"

Similar presentations


Ads by Google