1 / 23

Computer Programming 2

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. و من يتقِ الله . ألا تكفيك هذه ؟!

kalona
Download Presentation

Computer Programming 2

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. 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

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

  3. 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

  4. 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

  5. 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

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

  7. String in Java • String Class • StringBuffer Class • StringTokenizer Class Downloaded from http://staff.cst.ps/mfarra

  8. Class String • Class String is a pre-defined class in Java • Class Stringis 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

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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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 0 Downloaded from http://staff.cst.ps/mfarra

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

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

  23. Next Lecture … Class StringBuffer Downloaded from http://staff.cst.ps/mfarra

More Related