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