1 / 20

Collage of Information Technology University of Palestine, Gaza Prepared by:

A dvanced P rogramming. Lecture 16: Working with Text (1). Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra. قالوا. قال رسول الله صلى الله عليه و سلم: منْ تَعَجَّلَ الجَوابَ حَادَ عَنِ الصَوابِ. Outlines. Why working with Text ?

tress
Download Presentation

Collage of Information Technology University of Palestine, Gaza Prepared by:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  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. How does Java support working with Text? • The Java Application Programming Interface (API) contains two packages which are: • java.text : contains the international text classes. • java.util.regex: contains classes to work with regular expressions • And the packagejava.lang which contains the basic language classesasStringclass

  6. String class • Many methods: • String.valueOf( ); • Thing.toString( ); • equals( ); • compareTo( ); // method compares strings strictly by their characters' positions in the Unicode specification.

  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. String class/search • The String class provides several simple methods for finding fixed substrings within a string. • startsWith( ):Compares an argument string with the beginning of the String • endsWith( ):Compares an argument string with end of the String • 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:

  9. String class/search lastIndexOf( ) :searches backward through the string for the last occurrence of a character or substring. 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. String class/Editing • A number of methods operate on the String and return a new String as a result. • trim( ):Removes leading and trailing whitespace (i.e., carriage return, newline, and tab) from the String. • toUpperCase( ) and toLowerCase( ) methods return a new String of the appropriate case • substring( ):returns a specified range of characters.

  11. String class/Editing • replace( ): One or more occurrences of the target string are replaced with the replacement string, moving from beginning to end.

  12. String methods

  13. String methods

  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. 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. 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. Parse this by the following code 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. single whitespace character single whitespace character

  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. 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. Next Lecture isa Regular Expressions

More Related