Characters and Strings - PowerPoint PPT Presentation

About This Presentation
Title:

Characters and Strings

Description:

... is a sequence of characters treated as a single value. String s = new String('xyz'); Its special because you can create them many different ways: String s = 'xyz' ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 9
Provided by: mccl8
Category:

less

Transcript and Presenter's Notes

Title: Characters and Strings


1
  • Characters and Strings

2
Characters
  • New primitive char
  • char letter
  • letter a
  • char letter2 C
  • Because computers can only represent numbers,
    Characters have ASCII values

3
Characters
4
Characters
  • These values can be used in comparisons and in
    calculations
  • char letter a c
  • char letter (char) 70
  • int valueOfX (int) X
  • a lt b

5
Strings
  • Strings are special objects
  • A String is a sequence of characters treated as a
    single value
  • String s new String(xyz)
  • Its special because you can create them many
    different ways
  • String s xyz
  • String s inputBox.getString( )
  • s ABC

6
Strings
  • Strings are special objects
  • A String is a sequence of characters treated as a
    single value
  • String s new String(xyz)
  • Its special because you can create them many
    different ways
  • String s new String(xyz)
  • String s new String(inputBox.getString( ) )
  • s new String( s ABC)

7
Strings
  • Common String methods
  • length( ) - returns the length of the String
  • charAt(int i) returns char at position I
  • CompareTo(String s) 0 if equal, negative if
    this String is lt s, positive if this String gt s
  • Substring(int beginIndex, int endIndex) returns
    a new String
  • equals(String s) true or false
  • equalsIgnoreCase(String s) true or false
  • Know these!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

8
Strings
  • Example
  • // count the number of letter as in a string
  • int count 0
  • char a a
  • String s inputBox.getString()
  • for (int i 0 I lt s.length( ) i)
  • if (s.charAt( i ) a)
  • count
Write a Comment
User Comments (0)
About PowerShow.com