200 likes | 308 Views
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 ?
E N D
Advanced Programming Lecture 16: Working with Text (1) Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra
قالوا قال رسول الله صلى الله عليه و سلم: منْ تَعَجَّلَ الجَوابَ حَادَ عَنِ الصَوابِ
Outlines • Why working with Text ? • How Java supports working with Text? • String class and its methods • The Collator class • Parsing
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.
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
String class • Many methods: • String.valueOf( ); • Thing.toString( ); • equals( ); • compareTo( ); // method compares strings strictly by their characters' positions in the Unicode specification.
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.
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:
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
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.
String class/Editing • replace( ): One or more occurrences of the target string are replaced with the replacement string, moving from beginning to end.
StringBuilder • The java.lang.StringBuilder class is a modifiable and expandable buffer for characters. You can use it to create a big string efficiently.
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:
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.
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
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:
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.
Next Lecture isa Regular Expressions