230 likes | 323 Views
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. و من يتقِ الله . ألا تكفيك هذه ؟!
E N D
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
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
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
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
ASCII Character Set A = 65 Downloaded from http://staff.cst.ps/mfarra
String in Java • String Class • StringBuffer Class • StringTokenizer Class Downloaded from http://staff.cst.ps/mfarra
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
String Constructors • Class String provides constructors for initializing String objects in a variety of ways. Downloaded from http://staff.cst.ps/mfarra
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
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
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
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
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
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
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
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
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
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
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
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
valueOf Case Study Textbook chapter 29 Page 1365 Downloaded from http://staff.cst.ps/mfarra
Next Lecture … Class StringBuffer Downloaded from http://staff.cst.ps/mfarra