Title: String
1String Exception
2Lesson plan
- Class Object
- String
- Exercise for midterm
3Class Object
- A class definition provides a description of a
typical object within that class. - An individual object is an instance of a class.
- A class has its behavior (methods) and attributes
(fields).
4Class Object
- Access a dynamic method or field by
- ltobject namegt.ltmethod namegt
- ltobject namegt.ltfield namegt
- Example
- df.format(loanAmount)
- . Access a static method by
- ltclass namegt.ltmethod namegt
- Example
- JOptionPane. showInputDialog(null,"Loan Amount
(dollars.cents)")
5String
- What is it?
- A String is a sequence of characters that is
treated as a single value - String class handles all operations related to
String - String class is defined in java.lang.
6Create a String object
- Syntax
- String ltvariable namegt
- ltvariable namegtnew String(constant)
- OR
- String ltvariable namegt
- ltvariable namegt constant
7Create a String object
- Example
- String strVar
- strVar new String(CS 172 Course)
- OR
- String strVar
- strVar CS 172 Course
8Internal Representation of a String
Individual characters in a string object are
indexed from 0
9Compute Length of a string
- Method length()
- Returns the length of a string
- Example
- String strVar
- strVar new String(CS 172 Course)
- int len strVar.length()
10Substring
- Method
- Extract a substring from a given string by
specifying the beginning and ending positions - Example
- String strVar, strSubStr
- strVar new String(CS 172 Course)
- strSubStr strVar.substring(0,6)
strSubStr CS 172
11Index position of a substring within another
string
- Method
- Find an index position of a substring within
another string. - Example
- String strVar1 CS 172 Course
- String strVar2 Course
- int index
-
- index strVar1.indexOf(strVar2)
index 7
12Index position of a substring within another
string
- Example
- String strVar1 CS 172 Course
- String strVar2 C
- int index
-
- index strVar1.indexOf(strVar2)
index 0
13String concatenation
- Method
- Create a new string from two strings by
concatenating the two strings. - Example
- String strVar1 CS 172
- String strVar2 Course
- String sumStr
-
- sumStr strVar1strVar2
14Practice exercise
- Determine values of some variables given codes
15String comparison
- Two methods
- equals
- equalsIgnoreCase
- string1.equals(string2)
- Or
- string1.equalsIgnoreCase(string2)
16String comparison
- Methods
- equals
- equalsIgnoreCase
- compareTo
- String string1 CS 172
- String string2 172
- Boolean isEqual
- isEqual string1.equals(string2)
isEqual false
17String comparison
- equalsIgnoreCase
-
- Example
- String string1 COMPSCI
- String string2 compsci
- Boolean isEqual
- isEqual string1.equals(string2)
isEqual true
18String comparison
- compareTo
-
- Example
- String string1 Adam
- String string2 Brian
- int compareResult
- compareResult string1.compareTo(string2)
compareResult lt 0
19String comparison
- - string1.compareTo(string2)
- Compares two strings lexicographically
- will return 0 if two strings are equal
- will return negative value if string1 is less
than string 2 - will return positive value if string1 is
greater than string 2 - The comparison is based on the Unicode value of
each character in the strings -
20String comparison
- The comparison is based on the Unicode value of
each character in the strings -
- let k be the smallest index valid for both
strings - compareTo returns the difference of the two
character values at position k in the two string
-- that is, the value - character at the position k of string 1
character at the position k of string 2 -
21Character at position k of a string
- Example
- String sampleCS 172 Course
- char aChar
- aChar sample.charAt(3)
aChar1
22Character at position k of a string
- Example
- String sampleCS 172 Course
- char aChar
- aChar sample.charAt(3)
aChar1
23Implicit type of conversion
- Implicit type conversion is common
- Example
- int num 172
- String s CS "num
sCS 172
24Practice (answers)Monday (3)
int sum1 0 for (int i0 ilt 5 i) for (int
j0 jlt 5 j) sum1i System.out.println
(" Sum1 is " sum1)
i1 j0 -gt sum1 0i011 j1 -gt sum1
01 i 012 j5 -gt sum1 016 6
i0 j0 -gt sum1 0i 00 0 j1
-gt sum1 0i 00 0 .. j5 -gt sum1
0i 00 0
25Practice (answers)Monday (3)
i2 j0 -gt sum1 6i 621 j1
-gt sum1 622 622 .. j5 -gt sum1
6i 626 18
i3 j0 -gt sum1 183 1831 j1
-gt sum1 1833 1832 .. j5 -gt sum1
1836 36
i4 j0 -gt sum1 3641 j1 -gt
sum1 3642 .. j5 -gt sum1 3646
60
i5 j0 -gt sum1 6051 j1 -gt
sum1 6052 .. j5 -gt sum1 6056
90
SUM1 90
26Practice (answers)Wednesday (3)
int sum 0 int j0 while (jlt11)
j int i5 while (igtj) sum sum
(ij) i-- System.out.println( Sum
sum)
27Practice (answers)Wednesday (3)
j0 j 1 i5 sum sum (15) 6 i4 sum
sum (14) 65 11 i3 sum sum (13)
114 15 i2 sum sum(12) 153 18
28Practice (answers)Wednesday (3)
j 2 i5 sum sum (25) 187
25 i4 sum sum (24) 25 6 31 i3 sum
sum (23) 315 36
29Practice (answers)Wednesday (3)
j 3 i5 sum sum (35) 368
44 i4 sum sum (34) 44 7 51
30Practice (answers)Wednesday (3)
j 4 i5 sum sum (45) 519 60
31Practice
Write a loop that prints out a string in
reverse. input CS 172 output 271 SC
32Practice
public class StringReverse public static void
main(String args) String strSource "CS
172" String strDes"" int len
strSource.length()-1 for (int ilen igt0
i--) strDes strDes strSource.charAt(i)
System.out.println(" Reserve string is "
strDes)
33Practice
Multiple choices Determine the output of the
codes
34StringBuffer class
- A String object is immutable. Once it is created,
we cant add/delete/modify characters of a String
object - If we need to modify the content of a string
directly, we must use StringBuffer class
35Create a StringBuffer object
- StringBuffer word new StringBuffer(Java)
- word.setCharAt(0,D)
- word.setCharAt(1,i)
word DIVA
36Create a StringBuffer object
- StringBuffer word new StringBuffer(Java)
- word.setCharAt(0,D)
- word.setCharAt(1,i)
word DIVA
37Delete a substring from a StringBuffer object
- StringBuffer word new StringBuffer(CS172
Course) - word.delete(0,1)
-
word 172 Course
38Append a string
- StringBuffer word new StringBuffer(CS172)
- word.append(Course)
-
word 172 Course
39Insert a string
- StringBuffer word new StringBuffer(CS
Course) - word.insert(3,172)
-
word CS 172 Course
40Practice
- Write a method to sort three strings
alphabetically
41Practice
- Identify objects, class
- What will be the output from the following code?