Download
string n.
Skip this Video
Loading SlideShow in 5 Seconds..
String PowerPoint Presentation

String

144 Vues Download Presentation
Télécharger la présentation

String

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. String

  2. Lesson plan • Class & Object • String • Exercise for midterm

  3. Class & 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).

  4. Midterm exam • Date: Monday, February 27, 2005, McGraw 115 • Overview: - Data type - Assignment statement - If, nested if - Switch - Do-while, While, and For • Format: - Multiple choice questions - Given the code/expressions/statements.., detect the errors - Given the code, determine the results - Identify Class and Object from the code

  5. Class & Object • Access a dynamic method or field by: <object name>.<method name> <object name>.<field name> Example: df.format(loanAmount) . Access a static method by <class name>.<method name> Example: JOptionPane. showInputDialog(null,"Loan Amount (dollars.cents):");

  6. String • 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.

  7. Create a String object • Syntax: String <variable name>; <variable name>=new String(“constant”); OR String <variable name>; <variable name> = “constant”

  8. Create a String object • Example: String strVar; strVar = new String(“CS 172 Course”); OR String strVar; strVar = “CS 172 Course”;

  9. Internal Representation of a String Individual characters in a string object are indexed from 0

  10. Compute 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();

  11. Substring • 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”

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

  13. Index position of a substring within another string • Example: String strVar1 = “CS 172 Course”; String strVar2 = “C”; int index; index = strVar1.indexOf(strVar2); index = 0

  14. String 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 = strVar1+strVar2;

  15. Practice exercise Determine values of some variables given codes

  16. String comparison Two methods: equals equalsIgnoreCase string1.equals(string2) Or string1.equalsIgnoreCase(string2)

  17. String comparison Methods: equals equalsIgnoreCase compareTo String string1 =“CS 172”; String string2 = “172” Boolean isEqual; isEqual = string1.equals(string2); isEqual= false

  18. String comparison equalsIgnoreCase Example: String string1 =“COMPSCI”; String string2 = “compsci” Boolean isEqual; isEqual = string1.equals(string2); isEqual= true

  19. String comparison compareTo Example: String string1 =“Adam”; String string2 = “Brian” int compareResult; compareResult = string1.compareTo(string2); compareResult < 0

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

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

  22. Character at position k of a string Example: String sample=“CS 172 Course”; char aChar; aChar = sample.charAt(3) aChar=‘1’

  23. Character at position k of a string Example: String sample=“CS 172 Course”; char aChar; aChar = sample.charAt(3) aChar=‘1’

  24. Implicit type of conversion Implicit type conversion is common: Example: int num = 172; String s = “CS "+num; s=“CS 172”

  25. Practice Determine results of the code

  26. StringBuffer class • A String object is immutable. Once it is created, we can’t add/delete/modify characters of a String object • If we need to modify the content of a string directly, we must use StringBuffer class

  27. Create a StringBuffer object • StringBuffer word = new StringBuffer(“Java”); word.setCharAt(0,’D’); word.setCharAt(1,’i’); word = Diva

  28. Delete a substring from a StringBuffer object • StringBuffer word = new StringBuffer(“CS172 Course”); word.delete(0,1); word = “S172 Course”

  29. Append a string • StringBuffer word = new StringBuffer(“CS172”); word.append(“ Course”); word = “CS172 Course”

  30. Insert a string • StringBuffer word = new StringBuffer(“CS Course”); word.insert(3,“172 ”); word = “CS 172 Course”

  31. Convert from StringBuffer to String StringBuffer word = new StringBuffer(“Java”); word.setCharAt(0,’D’); word.setCharAt(1,’i’); System.out.println(word.toString());

  32. Practice Write a loop that prints out a string in reverse. For example: input: “CS 172” output: “271 SC” (This is just an example to show how the output should look like given a specific input value)