Download presentation
Presentation is loading. Please wait.
1
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
2
(إِنَّ اللَّهَ مَعَ الَّذِينَ اتَّقَوْا وَالَّذِينَ هُمْ مُحْسِنُونَ)
و من يتقِ الله ... ألا تكفيك هذه ؟! (إِنَّ اللَّهَ مَعَ الَّذِينَ اتَّقَوْا وَالَّذِينَ هُمْ مُحْسِنُونَ) (النحل:128) شريحـة ثابتـة لعلنا نحسن من خلالها أخلاقنـا و أفعالنا لنفوز يوم الامتحان الحقيقي Downloaded from Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
6
ASCII Character Set A = 65 Downloaded from http://staff.cst.ps/mfarra
Mahmoud Rafeek Alfarra
7
String in Java String Class StringBuffer Class StringTokenizer Class
Downloaded from Mahmoud Rafeek Alfarra
8
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 Mahmoud Rafeek Alfarra
9
String Constructors Class String provides constructors for initializing String objects in a variety of ways. Downloaded from Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Downloaded from Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
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 Mahmoud Rafeek Alfarra
22
Case Study valueOf Textbook chapter 29 Page 1365
Downloaded from Mahmoud Rafeek Alfarra
23
Class StringBuffer Next Lecture …
Downloaded from Mahmoud Rafeek Alfarra
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.