advanced programming collage of information technology university of palestine, gaza prepared by:...

20
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by : Mahmoud Rafeek Alfarra Lecture 16: Working with Text (1)

Upload: clifford-cain

Post on 25-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

Advanced Programming

Collage of Information

Technology University of Palestine, Gaza

Prepared by:

Mahmoud Rafeek Alfarra

Lecture 16: Working with Text (1)

Page 2: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

2

قالوا

قال رسول الله صلى الله عليه و سلم:

واَب� َل� الَج� واَب� مْن� َت�َع�َج� َل� الَج� مْن� َت�َع�َج��واَب اَد� ع�ْن� الَص� واَب�َح� اَد� ع�ْن� الَص� َح�

Page 3: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

3

Outlines

Why working with Text ?

How Java supports working with Text?

String class and its methods

The Collator class

Parsing

Page 4: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

4

Introduction

Organizations see the Internet and the Web as crucial to their information-systems strategies.

Java provides a number of built-in networking capabilities.

Java can enable programs to search the world for information and to collaborate with programs running on other computers internationally.

Page 5: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

5

How does Java support working with Text?

The Java Application Programming Interface (API) contains two packages which are:

1.java.text : contains the international text classes.

2.java.util.regex: contains classes to work with regular expressions

And the package java.lang which contains the basic language classes as String class

Page 6: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

6

String class

Many methods:

1.String.valueOf( );

2.Thing.toString( );

3.equals( );

4.compareTo( ); // method compares strings strictly by their characters' positions in the Unicode specification.

Page 7: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

7

The Collator class

The java.text package provides a sophisticated set of classes for comparing strings in specific languages.

You can obtain a default Collator by calling the Collator.getInstance( ) method with no arguments.

Once you have an appropriate Collator instance, you can use its compare( ) method, which returns values just like String's compareTo( ) method.

Using collators is essential if you're working with languages other than English.

Page 8: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

8

String class/search

The String class provides several simple methods for finding fixed substrings within a string.

1. startsWith( ): Compares an argument string with the beginning of the String

2. endsWith( ): Compares an argument string with end of the String

3. indexOf( ): Searches for the first occurrence of a character or substring and returns the starting character position, or -1 if the substring is not found:

Page 9: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

9

String class/search

4. lastIndexOf( ) : searches backward through the string for the last occurrence of a character or substring.

5. contains( ) : checking to see whether a given substring is contained in the target string

For more complex searching, you can use the Regular Expression API

We will study RegExp in the next lecture isa

Page 10: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

10

A number of methods operate on the String and return a new String as a result.

1. trim( ): Removes leading and trailing whitespace (i.e., carriage return, newline, and tab) from the String.

2. toUpperCase( ) and toLowerCase( ) methods return a new String of the appropriate case

3. substring( ): returns a specified range of characters.

String class/Editing

Page 11: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

11

4. replace( ): One or more occurrences of the target string are replaced with the replacement string, moving from beginning to end.

String class/Editing

Page 12: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

12

String methods

Page 13: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

13

String methods

Page 14: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

14

StringBuilder

The java.lang.StringBuilder class is a modifiable and expandable buffer for characters. You can use it to create a big string efficiently.

Page 15: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

15

Parsing Primitive Numbers Java has a rich set of APIs for parsing and printing

formatted strings, including numbers, dates, times, and currency values.

For numbers and Booleans, Each of these primitive wrapper classes has a static "parse" method that reads a String and returns the corresponding primitive type. For example:

Page 16: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

16

Parsing Primitive Numbers java.util.Scanner provides a single API for not

only parsing individual primitive types from strings, but reading them from a stream of tokens.

Page 17: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

17

Parsing a string of text A common programming task involves parsing a

string of text into words or "tokens" that are separated by some set of delimiter characters, such as spaces or commas.

Parse this by the following codesingle whitespace character

single whitespace character

Page 18: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

18

Parsing a string of text With the new Scanner API, we could go a step

further and parse the numbers of our second example as we extract them:

Page 19: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

19

Parsing a string of text StringTokenizer allows you to specify a delimiter

as a set of characters and matches any number or combination of those characters as a delimiter between tokens.

Page 20: Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text

20

Next Lecture isa

Regular Expressions