Presentation is loading. Please wait.

Presentation is loading. Please wait.

University of Central Florida COP 3330 Object Oriented Programming

Similar presentations


Presentation on theme: "University of Central Florida COP 3330 Object Oriented Programming"— Presentation transcript:

1 University of Central Florida COP 3330 Object Oriented Programming

2 Agenda Characters Strings RegEx

3 Characters

4 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

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

6 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

7 Strings

8 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

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

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

11 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

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

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

14 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

15 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…

16 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

17 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

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

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

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

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

22 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

23 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

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

25 Regular Expressions

26 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

27 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”

28 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

29 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

30 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

31 Regular Expressions


Download ppt "University of Central Florida COP 3330 Object Oriented Programming"

Similar presentations


Ads by Google