Robert Stumpf, Professor - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Robert Stumpf, Professor

Description:

Concatenation. It is defined as the addition or joining of two ... Strings are easy to create and concatenate. There are many methods that work with strings ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 14
Provided by: calpoly
Category:

less

Transcript and Presenter's Notes

Title: Robert Stumpf, Professor


1
Strings
  • Robert Stumpf, Professor
  • Computer Information Systems
  • California State Polytechnic University Pomona

2
Strings
  • Basics
  • Concatenation
  • Behaviors

3
Basics
  • Not all languages have a version of strings
  • For example C does not have them
  • A String is not a primitive data type
  • A String is an example of an object

4
Basics
  • An object is a collection of primitive data 1
  • Thus a String is a collection of characters
  • An object has behaviors 1 Note that an object
    may also contain other objects

5
Basics
  • The proper way to create a string isString
    myString new String ( abcd)
  • An easier way and more popular way isString
    myString abcd

6
Concatenation
  • It is defined as the addition or joining of two
    strings into a new String
  • For example
  • String myString abc def
  • String name firstName lastName
  • myString has the value of abcdef

7
Concatenation
  • Java allows automatic data type conversions in
    the concatenation operation
  • For example
  • double number 1000.0
  • String myString The answer is number
  • This is allowed so displaying answers will be
    easy to do to obtain The answer is 1000.0
  • The string conversion of number is implicit as
    long as it is not the first argument

8
Behavior
  • One can get extract a String or a Character
  • String s1 abcdef
  • String s2 s1.substring (2) s1 has the
    value of cdef
  • String s2 s1.substring (2,4) s2 has
    the value of cd
  • char c1 s1.charAt (2) c1 has the value
    of c

9
Behavior
  • There are many methods, for example
  • String s1 abcdef int temp s1.indexOf
    (c)
  • temp has the value of 2
  • Note that the argument is a character

10
Behavior
  • A String may not be changed
  • But a new different string can be created
  • For example
  • String s1 Abcd
  • String s2 s1.toLowerCase ( )
  • String s3 s1.toUpperCase ( )
  • s2 has the value of abcd
  • s3 has the value of ABCD

11
Behavior
  • It is important that the operators of !
    not be used in the normal way
  • s1.equals (s2)
  • not s1.equalsIgnoreCase (s2)
  • also one may use
  • int temp s1.compares (s2)
  • temp 0 //
  • temp! 0 // !

12
Summary
  • Remember Strings are an object
  • An object consists of many primitives which in
    this case are characters
  • Strings are easy to create and concatenate
  • There are many methods that work with strings

13
Thank You
  • Strings Make it Easy to Work with English like
    Data
  • Any questions should be directed to Professor
    Robert Stumpf
  • Email rvstumpf_at_csupomona.edu
  • Web Sitehttp//www.csupomona.edu/rvstumpf
Write a Comment
User Comments (0)
About PowerShow.com