Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.

Similar presentations


Presentation on theme: "Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION."— Presentation transcript:

1 Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE

2 و من يتقِ الله... ألا تكفيك هذه ؟ ! ( إِنَّ اللَّهَ مَعَ الَّذِينَ اتَّقَوْا وَالَّذِينَ هُمْ مُحْسِنُونَ ) ( النحل :128) Mahmoud Rafeek Alfarra 2 Downloaded from http://staff.cst.ps/mfarra شريحـة ثابتـة لعلنا نحسن من خلالها أخلاقنـا و أفعالنا لنفوز يوم الامتحان الحقيقي

3 Out Lines Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 3 Class StringTokenizer  Introduction  Constructors  Count Tokens  Print Tokens  Token Processing

4 Class StringTokenizer: Introduction Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 4 When you read a sentence, your mind breaks the sentence into tokens individual words and punctuation marks, each of which conveys meaning to you. Tokens are separated from one another by delimiters, typically whitespace characters such as space, tab, newline and carriage return

5 Class StringTokenizer: Constructors Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 5 StringTokenizer(String str) String x = “Mahmoud Rafeek Alfarra” StringTokenizer tokens = new StringTokenizer( x ); String x = “Mahmoud Rafeek Alfarra” StringTokenizer tokens = new StringTokenizer( x );

6 Class StringTokenizer: Constructors Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 6 StringTokenizer(String str, String delim) String x = "2005/12/15“ StringTokenizer tokens = new StringTokenizer(x, "/"); String x = "2005/12/15“ StringTokenizer tokens = new StringTokenizer(x, "/");

7 Class StringTokenizer: Constructors Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 7 StringTokenizer(String str, String delim, boolean returnDelims) str The input String delimiters returnDelimiters Returns delimiters as tokens or skip them String x = "2005/12/15“ StringTokenizer tokens = new StringTokenizer(x, "/“, false); String x = "2005/12/15“ StringTokenizer tokens = new StringTokenizer(x, "/“, false);

8 Class StringTokenizer: CountToken Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 8 StringTokenizer has a countTokens() method which Calculates the reminder number of tokens in the string. String x = "2005/12/15“ StringTokenizer tokens = new StringTokenizer(x, "/"); int a = tokens.countToken(); // return 3 String x = "2005/12/15“ StringTokenizer tokens = new StringTokenizer(x, "/"); int a = tokens.countToken(); // return 3

9 Class StringTokenizer: PrintTokens Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 9 StringTokenizer has a hasMoreElements() and hasMoreTokens() methods which Test if there are more tokens available from this tokenizer's string. StringTokenizer has a nextElement() and nextToken() methods which returns the next token from this string tokenizer.

10 Class StringTokenizer: PrintTokens Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 10 String x = "I love islam So Much“; StringTokenizer st= new StringTokenizer(x); while (st.hasMoreElements()) { System.out.println("token = " + st.nextToken());

11 Class StringTokenizer: Token Processing Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 11 Using the methods of StringClass as toUpperCase toLowercase comparTo … we can process the tokens and store it in data structures

12 Example Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 12 import java.util.*; public class tokenPRoject { public static void main(String[] args) { String x = "i love islam so much"; StringTokenizer a = new StringTokenizer (x); while (a.hasMoreElements()){ String f = a.nextToken(); System.out.println(f.toUpperCase()); System.out.println(a.countTokens()); } System.out.println("=================="); String [] result = x.split("\\s"); for(int i = 0; i<result.length; i++ ) System.out.println(result[i].toUpperCase( )); } }

13 Next Lecture … Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 13 Class StringBuffer


Download ppt "Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION."

Similar presentations


Ads by Google