Advanced String handling

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Fundamental Programming Structures in Java: Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
1.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text.
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
BPJ444: Business Programming Using Java – String Handling Tim McKenna
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
The Scala API Application Programmer’s Interface.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 1 Regular Expressions and String processing Animated Version.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Chapter 7: Characters, Strings, and the StringBuilder.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Chapter 8 String Manipulation
Introduction to programming in java
String and StringBuffer classes
Computer Programming ||
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
String Processing Upsorn Praphamontripong CS 1110
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 6 The while Statement
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
Tonga Institute of Higher Education
16 Strings.
Microsoft Visual Basic 2005: Reloaded Second Edition
CS2011 Introduction to Programming I Strings
JavaScript: Objects.
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
String methods 26-Apr-19.
Strings in Java.
Visual Programming COMP-315
Exam Prep.
String Manipulation.
In Java, strings are objects that belong to class java.lang.String .
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

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()

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

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();

Splitting tokens Extracting information from a string Output January 23 1923 10 32 15 pm Splitting tokens Extracting information from a string Example: String date = "January 23, 1923 10: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

Another way to split tokens Output ( ( 3.5 + 52 ) / 234 + 75.2 * 83.9 - 9.0 Another way to split tokens String Tokenizer class String expression = "((3.5 + 52)/234 + 75.2*83.9 - 9.0"; // 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