Part a: Fundamentals & Class String

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Fundamental Programming Structures in Java: Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 16: Working with Text.
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Lecture 14 March 23, Exam Results Class Average was 69.4 –Median was 72.5 (which means there were some very low grades) Questions 31, 32, and 33.
Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
JavaScript Objects.
EKT 472: Object Oriented Programming
String and String Buffers
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Introduction To Programming Information Technology , 1’st Semester
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
Tonga Institute of Higher Education
Java – String Handling.
Introduction To Programming Information Technology , 1’st Semester
Lecture 07 String Jaeki Song.
CS2011 Introduction to Programming I Strings
JavaScript: Objects.
Strings in Java.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Exam Prep.
Switch, Strings, and ArrayLists in Java
In Java, strings are objects that belong to class java.lang.String .
Pre-AP® Computer Science Quiz
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

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

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

Out Lines Fundamentals of Characters and Strings Class String String Constructors String Methods length, charAt and getChars Comparing Strings Locating Characters and Substrings in Strings Extracting Substrings from Strings Concatenating Strings Miscellaneous String Methods String Method valueOf Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Fundamentals of Characters and Strings Every program is composed of a sequence of characters thatwhen grouped together meaningfullyare interpreted by the computer as a series of instructions used to accomplish a task. A character literal is an integer value represented as a character in single quotes. Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Fundamentals of Characters and Strings The value of a character literal is the integer value of the character in the Unicode character set. String literals are stored in memory as String objects. Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

ASCII Character Set A = 65 Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

String in Java String Class StringBuffer Class StringTokenizer Class Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Class String Class String is a pre-defined class in Java Class String is used to represent strings in Java which has many capabilities. We can declare an object of string with out Constructor as: String x = “Ahmad” Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

String Constructors Class String provides constructors for initializing String objects in a variety of ways. Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

length, charAt and getChars Class String has many operations to manipulate strings. Length: return the length of a string String y = "Mahmoud Rafeek Alfarra"; System.out.print(y.length()); Will print : 22 Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

length, charAt and getChars Class String has many operations to manipulate strings. charAt : obtain the character at a specific location in a string. String y = "Mahmoud Rafeek Alfarra"; System.out.println(y.charAt(0)); System.out.println(y.charAt(1)); Will print : M a Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

length, charAt and getChars Class String has many operations to manipulate strings. getChars: retrieve a set of characters from a string as a char array. char [] ret = new char [5] ; String y = "Mahmoud Rafeek Alfarra"; y.getChars(0,3,ret,0); Will stor mah in ret Will print m a h for(int i=0; i<5; i++) System.out.println(ret[i]); Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Comparing Strings Class String provides several methods for comparing strings. Equals: compare if two strings are identical. equalsIgnoreCase: ignores whether the letters in each string are uppercase or lowercase. compareTo: does not ignore the case regionMatches: compare portions of two strings for equality. Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Comparing Strings Class String provides several methods for comparing strings. Equals: String s1 = "Ali"; System.out.println(s1.equals("Ala")); Will print false Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Comparing Strings Class String provides several methods for comparing strings. equalsIgnoreCase String s1 = "Ali"; System.out.println(s1.equalsIgnoreCase("ali")); Will print true Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Comparing Strings Class String provides several methods for comparing strings. compareTo: does not ignore the case String s1 = "zain"; System.out.println(s1.compareTo("hussam")); Will print negative String s1 = “ahmad"; System.out.println(s1.compareTo("hussam")); Will print Positive String s1 = "zain"; System.out.println(s1.compareTo(“zain")); Will print Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Comparing Strings Class String provides several methods for comparing strings. regionMatches: String s1 = "Mahmoud"; System.out.println(s1.regionMatches(3,"lmoud",1,4)); Will print true Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Locating Characters and Substrings in Strings lastIndexOf and indexOf are methods to detect the position of substrings in any string. String s1 = "Mahmoud Rafeek Alfarra"; System.out.println(s1.lastIndexOf("a")); System.out.println(s1.indexOf("a")); Will print 21 1 Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Extracting Substrings from Strings Class String provides two substring methods to enable a new String object to be created by copying part of an existing String object. Each method returns a new String object. String s1 = "Mahmoud Rafeek Alfarra"; System.out.println(s1.substring(0,3)); System.out.println(s1.substring(12)); Will print Mah ek Alfarra Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Concatenating Strings String method concat concatenates two String objects and returns a new String object containing the characters from both original strings. String s1 = "Mahmoud "; String s2 = "Rafeek "; String s3 = s1.concat(s2); System.out.println(s3); Will print Mahmoud Rafeek Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Miscellaneous String Methods Replace: replace substring with substring toLowerCase: generate a new String object with lowercase letters toUpperCase: generate a new String object with uppercase letters . trim: removes all whitespace characters that appear at the beginning or end of the string toCharArray: to create a new character array containing a copy of the characters in string Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

Case Study valueOf Textbook chapter 29 Page 1365 Downloaded from http://staff.cst.ps/mfarra Mahmoud Rafeek Alfarra

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