Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text."— Presentation transcript:

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

2 2 قالوا قال رسول الله صلى الله عليه و سلم : منْ تَعَجَّلَ الجَوابَ حَادَ عَنِ الصَوابِ

3 3 Outlines  Why working with Text ?  How Java supports working with Text?  String class and its methods  The Collator class  Parsing

4 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.

5 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

6 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.

7 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.

8 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( ): S earches for the first occurrence of a character or substring and returns the starting character position, or -1 if the substring is not found:

9 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

10 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

11 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

12 12 String methods

13 13 String methods

14 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.

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

16 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.

17 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 code single whitespace character

18 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:

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

20 20 Next Lecture isa Regular Expressions


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

Similar presentations


Ads by Google