University of Central Florida COP 3330 Object Oriented Programming

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
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 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.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
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 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
Lecture 221 CS110 Lecture 22 Tuesday, April 20, 2004 Announcements –hw10 due Thursday, April 22 –exam next Tuesday, April 27 Agenda –Questions –Error handling.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
Strings Edward J. Biebel. Strings Strings are fundamental part of all computing languages. At the basic level, they are just a data structure that can.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
JAVA Programming (Session 5) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 9: Text Processing and More about Wrapper Classes Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Regular Expressions.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Regular Expressions – An Overview Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in.
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.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
10-2 Chapter 10 discusses the following main topics:  Introduction to Wrapper Classes  Character Testing and Conversion with the Character Class  More.
String Handling StringBuffer class character class StringTokenizer class.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Text Processing and More about Wrapper Classes Starting.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.
String class. Method concat Create string object –> String st1, st2; Input character to string object –> st1=br.readLine(); st2= br.readLine(); Use method.
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:
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
17-Feb-16 String and StringBuilder Part I: String.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Chapter 8 String Manipulation
Chapter 10: Text Processing and More about Wrapper Classes
The String class is defined in the java.lang package
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
EKT 472: Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
String class in java Visit for more Learning Resources string.
Advanced Programming Behnam Hatami Fall 2017.
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
String and StringBuilder
Java – String Handling.
String and StringBuilder
String methods 26-Apr-19.
Strings in Java.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 10 Thinking in Objects Part 2
JAVA – String Function PROF. S. LAKSHMANAN,
In Java, strings are objects that belong to class java.lang.String .
Unit-2 Objects and Classes
Presentation transcript:

University of Central Florida COP 3330 Object Oriented Programming

Agenda Characters Strings RegEx

Characters

Characters If you are using a single character value you will use the primitive data type char At times may need to use a char as an object Java provides a Character class that wraps the primitive data char Character has many methods for manipulating character data

Characters Some commonly used methods boolean isLetter(char ch) boolean isDigit(char ch) boolean isWhitespace(char ch) boolean isUpperCase(char ch) boolean isLowerCase(char ch) char toUpperCase(char ch) char toLowerCase(char ch) toString(char ch)

Characters Escape sequences is a character preceded by a backslash with a specific meaning to the compiler \t ~ tab \b ~ backspace \n ~ newline \r ~ carriage return \f ~ formfeed \’ ~ single quote \” ~ double quote \\ ~ backslash

Strings

Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings

Creating Strings The most direct was to create a string is to write: String greeting = “Hello COP 3330”; The text in the double quotes is called a string literal As with any other object, can create String objects by use the new keyword and a constructor call String data = new String();

Creating Strings The String class has 13 constructors to set the initial value of the string using different sources, such as a character array

String Accessor Methods Methods used to obtain information about an object are known as accessor methods Class String has a method called length() that returns the number of characters contained in the string object Class String has a method called getChars() that converts a string to an array of characters which would eliminate the need to loop through a string object to convert it to a char array

Concatenating Strings Method to concatenate two strings is concat() Can also use the + operator for explicit text, used frequently in System.out.println()

Format Strings Method format() that allows for formatting objects of the String class (similar to what C does) See source code example

Converting Numbers and Strings Many programs take input from a user in String format or number format and needs to be converted to the other type All of the Number classes wrap the primitive numeric types as a class byte class is Byte int class is Integer float class is Float double class is Double long class is Long short class is Short

Converting Numbers and Strings The Number classes provide a method called valueOf() that converts a string to the desired data type An alternative is to use the appropriate parse method of each number class such as Float.parseFloat(string); Integer.parseInt(string); Short.parseShort(string); Etc…

Converting Numbers and Strings Sometimes you need to convert a number to a string because you need to operate on the value in its string form. There are several easy ways to convert a number to a string. Cheap and easy (ghetto) method is to concatenate the numeric value with an empty string Use method String.valueOf() passing the number as an argument Use the Number class’s .toString() method

Manipulating Characters in Strings String class has a number of methods for examining the contents of strings, finding characters or substrings within a string, changing case, and other tasks substring() has two formats, one argument or two arguments one argument represents the beginning index only two arguments represents the beginning index and the ending index

Manipulating Characters in Strings Other useful methods for manipulating String[] split(String regex) String[] split(String regex, int limit) CharSequence subSequence(int beginIndex, int endIndex) String trim() String toLowerCase() String toUpperCase()

Manipulating Characters in Strings Other useful methods for searching int indexOf(int ch) int lastIndexOf(int ch) int indexOf(int ch, int fromIndex) int lastIndexOf(int ch, int fromIndex) int indexOf(String str) int lastIndexOf(String str) int indexOf(String str, int fromIndex) int lastIndexOf(String str, int fromIndex) boolean contains(CharSequence s)

Manipulating Characters in Strings Other useful methods for replacing String replace(char oldChar, char newChar) String replace(CharSequence target, CharSequence replacement) String replaceAll(String regex, String replacement) String replaceFirst(String regex, String replacement)

Manipulating Characters in Strings Other useful methods for comparing boolean endsWith(String suffix) boolean startsWith(String prefix) boolean startsWith(String prefix, int offset) int compareTo(String anotherString) int compareToIgnoreCase(String str) boolean equals(Object anObject) boolean equalsIgnoreCase(String anotherString) boolean regionMatches(int toffset, String other, int ooffset, int len) boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) boolean matches(String regex)

StringBuilder Class StringBuilder objects are similar to String objects except they can be modified Internally String Builder objects are treated like variable-length arrays The String class is preferred over StringBuilder unless StringBuilder offers an advantage over String in terms of simpler code Like the String class, StringBuilder has a length() method StringBuilder also has a capacity() method

StringBuilder Class StringBuilder has four constructors StringBuilder(CharSequence cs) StringBuilder(int initCapacity) StringBuilder(String s) Convenience methods in the StringBuilder include 10 append() method signatures 10 insert() method signatures

StringBuilder Class Convenience methods in the StringBuilder include StringBuilder delete(int start, int end) StringBuilder deleteCharAt(int index) StringBuilder replace(int start, int end, String s) void setCharAt(int index, char c) StringBuilder reverse() String toString()

Regular Expressions

Regular Expressions Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. They can be used to search, edit, or manipulate text and data. You must learn a specific syntax to create regular expressions Regular expressions vary in complexity, but once you understand the basics of how they're constructed, you'll be able to decipher (or create) any regular expression

Regular Expressions Can be a string literals like “foo” Would return true if the sequence of “foo” was located in a string Can be character literals like [bcr]at Would return true if the sequence of “bat”, “cat”, or “rat” was located in a string Can be character negation literals like [^bcr]at Would return true if the first letter was NOT b, c, or r but included at, such as “hat”

Regular Expressions Can be character literal ranges like [a-c] Would return true if the letters a, b, or c were found in the string Can be unions like [0-4[6-8]] Would return true for numbers 0 through 4 and six through 8 Would return false for number 5 and anything greater than 8 Can be intersections like [0-9&&[345]] Would return true for the value 3 Would return false for the value of 6

Regular Expressions Predefined classes . matches anything \d matches all digits \s matches spaces \w matches word characters \D matches non-digits \S matches non-spaces \W matches non-word characters

Regular Expressions Pattern class Matcher class CharSequence Represents a regular expression Matcher class Contains both a regular expression pattern and a CharSequence in which to search for the pattern CharSequence An interface that allows read access to a sequence of characters See source code example for implementation

Regular Expressions