Jay Robert Fernandez, M'Sc' - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Jay Robert Fernandez, M'Sc'

Description:

Jay Robert Fernandez, M.Sc. Mark Joseph Atienza. Instructors. Java sa Eskwela (JsE) Program ... String color = 'blue'; color is a String reference 'blue' is an ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 40
Provided by: jayrobertc
Category:
Tags: fernandez | jay | robert

less

Transcript and Presenter's Notes

Title: Jay Robert Fernandez, M'Sc'


1
Xavier University Computer Center Sun
Microsystems, Philippines
Java sa Eskwela (JsE) Program
  • Jay Robert Fernandez, M.Sc.
  • Mark Joseph Atienza
  • Instructors

2
Chapter 10
Strings and Characters
3
Chapter 10
  • Outline
  • Fundamentals of Characters and Strings
  • 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
  • Using String Method valueOf

4
Chapter 10
  • Outline
  • String Method intern
  • StringBuffer Class
  • StringBuffer Constructors
  • StringBuffer Methods length, capacity, setLength
    and ensureCapacity
  • StringBuffer Methods charAt, setCharAt, getChars
    and reverse
  • StringBuffer append Methods
  • StringBuffer Insertion and Deletion Methods

5
Objectives
  • To be able to create and manipulate
    non-modifiable character string objects of class
    String
  • To able to create and manipulate modifiable
    character string objects of class StringBuffer
  • To be able to create and manipulate objects of
    class Character

6
String and Characters
  • Characters
  • building block of Java source programs
  • groups of meaningful characters interpreted by
    computer as instructions
  • character constant integer represented as a
    character in single quotes (Unicode character
    set)
  • 'z' is integer value of z

7
String and Characters
  • Strings
  • group of characters treated as a single unit
  • may include letters, digits, special characters
    (,- ...)
  • Strings are objects of class String

8
String and Characters
  • Strings (continued)
  • String literal (string constants or anonymous
    String objects)
  • sequence of characters written in double quotes
  • "John Q. Doe" or "111222333"
  • Java treats all anonymous String objects with
    same contents as one object with many references

9
String and Characters
  • Strings (continued)
  • assigning strings
  • may assign in declaration
  • String color "blue"
  • color is a String reference
  • "blue" is an anonymous String object

10
String and Characters
  • Constructors for class String
  • s1 new String()
  • s1 is an empty string
  • s2 new String( anotherString )
  • s2 is a copy of anotherString
  • s3 new String( charArray )
  • s3 contains all the characters in charArray

11
String and Characters
  • Constructors for class String
  • s4 new String( charArray, offset, numElements )
  • s4 contains numElements characters from
    charArray, starting at location offset
  • s5 new String( byteArray, offset, numElements)
  • as above, but with a byteArray
  • s6 new String( byteArray )
  • s6 contains copy of entire byteArray

12
String and Characters
  • StringBuffer
  • dynamically resizable, modifiable string (more
    later)
  • StringBuffer buffer new StringBuffer("Hello
    there")
  • can initialize Strings with StringBuffers
  • s7 new String( myStringBuffer )

13
String and Characters
  • String methods
  • s1.length()
  • returns length of string
  • be careful, Strings do not have an instance
    variable length like arrays
  • s1.length is an error
  • s1.charAt( index )
  • returns char at location index
  • numbered like arrays (start at position 0)

14
String and Characters
  • String methods (continued)
  • s1.getChars( start, end, charArray, offset )
  • copies characters in s1 from index start to index
    end to location offset in charArray

15
String and Characters
  • Comparing Strings
  • all characters represented as numeric codes
    (Appendix D)
  • when computer compares strings, compares numeric
    codes
  • lexicographical comparison - compare Unicode
    values
  • lowercase different than uppercase

16
String and Characters
  • Comparison methods
  • s1.equals( "Hi" )
  • returns true if s1 lexicographically equal to
    "Hi"
  • capitalization (case) matters
  • s1.equalsIgnoreCase( otherString )
  • tests for equality, case does not matter
  • "hello" equal to "HELLO"

17
String and Characters
  • Comparison methods (continued)
  • s1.compareTo( otherString )
  • uses lexicographical comparison
  • returns 0 if strings equal
  • returns negative number if s1 lt otherString
  • returns positive number if s1 gt otherString

18
String and Characters
  • Comparison methods (continued)
  • s1.regionMatches( offset, otherString, start, end
    )
  • compares s1 from location offset to otherString
    from location start to end
  • s1.regionMatches( ignoreCase, offset,
    otherString, start, end )
  • as above, but if ignoreCase is true, case is
    ignored

19
String and Characters
  • operator
  • when used with primitive data types, returns true
    if equal
  • when used with references, returns true if point
    to same location in memory
  • remember, Java treats all anonymous String
    objects with same contents as one object with
    many references

20
String and Characters
  • operator
  • s1 "hello" //uses anonymous object
  • s2 new String("hello")
  • s1 "hello" //true
  • s2 "hello" //false

21
String and Characters
  • Search methods
  • s1.indexOf( integerRep )
  • integerRep - integer representation of a
    character ('z')
  • returns index of the first occurrence of the
    character in the string (returns -1 if not found)
  • s1.indexOf( integerRep, startSearch )
  • as above, but begins search at position
    startSearch

22
String and Characters
  • Search methods (continued)
  • s1.lastIndexOf( integerRep )
  • returns index of last occurrence of character in
    string
  • s1.lastIndexOf( integerRep, startSearch )
  • as above, but searches backwards from position
    startSearch

23
String and Characters
  • Search methods (continued)
  • can use indexOf and lastIndexOf with Strings
  • search for substrings within a string
  • identical usage as with characters, i.e.
  • s1.indexOf( "hi" )
  • s1.lastIndexOf( "this", 24 )

24
String and Characters
  • substring methods
  • return a String object
  • s1.substring( startIndex )
  • returns a substring from startIndex to the end of
    the string
  • s1.substring( start, end )
  • returns a substring from location start up to,
    but not including, location end
  • if index out of bounds, StringIndexOutOfBoundsExce
    ption generated

25
String and Characters
  • Method concat
  • concatenates two Strings, returns new String
    object
  • original Strings are not modified
  • s1.concat( s2 )
  • returns concatenation of s1 and s2
  • if no argument, returns s1

26
String and Characters
  • Miscellaneous methods
  • s1.replace( char1, char2 )
  • returns a new String, replacing all instances of
    char1 with char2
  • use integer representations 'z', '1', etc.
  • original String not changed
  • if no instances of char1, original string returned

27
String and Characters
  • Miscellaneous methods (continued)
  • s1.toUpperCase()
  • returns new String object with all letters
    capitalized (if no changes needed, original
    string returned)
  • original string unchanged
  • s1.toLowerCase() similar
  • s1.trim()
  • returns new String, removing all whitespace
    characters at beginning or end
  • original unchanged

28
String and Characters
  • Miscellaneous methods (continued)
  • s1.toString()
  • all objects can be converted to Strings with
    toString
  • if not overridden, default version creates a
    String with object's name and hashcode
  • s1.toCharArray()
  • creates a new character array containing copy of
    characters in s1

29
String and Characters
  • static methods of class String
  • take arguments and convert them to Strings
  • String.valueOf( charArray )
  • returns new String object containing characters
    in charArray
  • String.valueOf( charArray, startIndex, numChars )
  • as above, but starts from position startIndex and
    copies numChars characters

30
String and Characters
  • static methods of class String
  • other versions of valueOf take boolean, char,
    int, long, float, double, and Object
  • Objects converted to Strings with toString

31
String and Characters
  • Method intern
  • improves string comparison performance
  • returns reference guaranteed to have same
    contents
  • multiple interns generate references to same
    object
  • Strings can be compared with (same location in
    memory) instead of equals
  • much faster than comparing character by character

32
String and Characters
  • class String
  • once a String is created, it cannot be changed
  • class StringBuffer
  • can create modifiable Strings
  • capable of storing number of characters specified
    by capacity
  • if capacity exceeded, automatically expands to
    hold new characters
  • can use and operators

33
String and Characters
  • StringBuffer constructors
  • sb1 new StringBuffer()
  • creates empty StringBuffer, capacity of 16
    characters
  • sb2 new StringBuffer(capacity)
  • creates empty StringBuffer with specified
    capacity
  • sb3 new StringBuffer(myString)
  • creates a StringBuffer containing myString, with
    capacity myString.length() 16

34
String and Characters
  • StringBuffer methods
  • length returns length
  • capacity returns capacity
  • setLength( newLength )
  • changes length of StringBuffer to newLength
  • if too long, truncates excess characters
  • if too short, fills with null character (numeric
    representation of 0)

35
String and Characters
  • StringBuffer methods
  • ensureCapacity(size)
  • expands capacity to a minimum of size
  • if size less than current capacity, then capacity
    is unchanged

36
String and Characters
  • StringBuffer methods
  • charAt( index ) returns character at position
    index
  • setCharAt( index, char )
  • sets character at position index to char
  • getChars( start, end, charArray, loc )
  • copies from position start up to (not including)
    end into location loc in charArray
  • reverse reverses contents of StringBuffer

37
String and Characters
  • Tokenization
  • breaking statements into pieces
  • class StringTokenizer breaks a String into
    component tokens
  • tokens separated by delimiters, typically white
    space characters
  • other characters may be used

38
String and Characters
  • Class StringTokenizer
  • one constructor StringTokenizer( myString )
  • initialize StringTokenizer with a String
  • default delimiter " \n\t\r" (space, newline, tab,
    carriage return)
  • other constructors allow you to specify delimiter

39
String and Characters
  • Methods
  • countTokens()
  • determines number of tokens in String
  • nextToken()
  • returns next token as a String
  • nextToken( delimiterString )
  • changes delimiter while tokenizing a String
  • hasMoreTokens()
  • determines if there are more tokens to be
    tokenized
Write a Comment
User Comments (0)
About PowerShow.com