computer programming 2 lecture 5: string processing part b: class stringtokenizer prepared &...

13
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

Upload: philomena-douglas

Post on 03-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Computer Programming 2

Lecture 5:String Processing

Part b: Class StringTokenizer

Prepared & Presented by: Mahmoud Rafeek

Alfarra

MINISTRY OF EDUCATION & HIGHER EDUCATIONCOLLEGE OF SCIENCE AND TECHNOLOGY (CST)KHANYOUNIS- PALESTINE

و من يتق� الله ...

أال تكفيك هذه ؟!

ع�)إ�ن� الل�ه� ال�ذ�ين� م�ا و ال�ذ�ين� ه$م ات�ق� و�

) ن$ون� س� م$ح (128)النحل:

Mahmoud Rafeek Alfarra

2

Downloaded from http://staff.cst.ps/mfarraDownloaded from http://staff.cst.ps/mfarra

شريحـة ثابتـة لعلنا نحسن من خاللها أخالقنـا و أفعالنا لنفوز يوم االمتحان الحقيقي

Out Lines Out Lines

Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra

3

Class StringTokenizer Introduction Constructors Count Tokens Print Tokens Token Processing

Class StringTokenizer: IntroductionClass StringTokenizer: Introduction

Mahmoud Rafeek AlfarraDownloaded 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

Class StringTokenizer: ConstructorsClass StringTokenizer: Constructors

Mahmoud Rafeek AlfarraDownloaded 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 );

Class StringTokenizer: ConstructorsClass StringTokenizer: Constructors

Mahmoud Rafeek AlfarraDownloaded 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

, "/");

Class StringTokenizer: ConstructorsClass StringTokenizer: Constructors

Mahmoud Rafeek AlfarraDownloaded 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);

Class StringTokenizer: CountTokenClass StringTokenizer: CountToken

Mahmoud Rafeek AlfarraDownloaded 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

Class StringTokenizer: PrintTokensClass StringTokenizer: PrintTokens

Mahmoud Rafeek AlfarraDownloaded 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.

Class StringTokenizer: PrintTokensClass StringTokenizer: PrintTokens

Mahmoud Rafeek AlfarraDownloaded 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());

Class StringTokenizer: Token ProcessingClass StringTokenizer: Token Processing

Mahmoud Rafeek AlfarraDownloaded 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

ExampleExample

Mahmoud Rafeek AlfarraDownloaded 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( )); } }

Next LectureNext Lecture… …

Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra

13

Class

StringBuffer