Strings in Java - PowerPoint PPT Presentation

About This Presentation
Title:

Strings in Java

Description:

They are similar to an array of characters in that they represent an ... String constants are enclosed in double quotes. Example: String message = 'Hello World! ... – PowerPoint PPT presentation

Number of Views:4024
Avg rating:3.0/5.0
Slides: 10
Provided by: alanwi8
Category:

less

Transcript and Presenter's Notes

Title: Strings in Java


1
Strings in Java
  • Strings in Java are also a reference type.
  • They are similar to an array of characters in
    that they represent an ordered sequence of
    characters with positions numbered from 0.
  • String constants are enclosed in double quotes.
  • Example
  • String message "Hello World!"
  • System.out.println( message )
  • The class String provides many useful methods.

2
Comparison with character arrays
  • Although a String has many similarities to an
    array of characters, there are some important
    differences.
  • You dont need to use new to create a string
    unless you want to use one of the String class
    constructors
  • String s1 "Hello!"
  • String s2 new String("Hello!")
  • You DO NOT use to access the characters in the
    string.
  • The contents of a String cannot be changed after
    it has been created.
  • You can have a String variable refer to a
    different string, but you cannot change
    characters within the original string.
  • There is NO equivalent to
  • char x new char 'h','e','l','l','o'
  • x2 'q'

3
Conversion to/from character arrays
  • To convert from a character array to a String
  • char x new char 'h','e','l','l','o'
  • String s1 new String( x )
  • To convert from a String to a character array
  • char x aString.toCharArray( )

4
Useful String methods
  • Suppose we have
  • String message "Hello World!"
  • To find the length of a string
  • int theStringLength message.length()
  • To find the character at position i (numbered
    from 0)
  • int i 4
  • char theChar message.charAt( i )

5
Useful String methods
  • To change any primitive data type to a String
  • int anInteger 17
  • String aString String.valueOf( anInteger )
  • To append one string after another
    (concatenation)
  • String joinedString string1 string2

6
Useful String methods
  • To find a particular character or String within a
    String
  • First occurrence of 'x'
  • int location aString.indexOf('x')
  • First occurrence of "world" starting from
    position pos
  • int location2 aString.indexOf( "world", pos )
  • There is also a corresponding method lastIndexOf(
    ) occurrence of "world" starting from position
    pos

7
Useful String methods
  • Substrings
  • The substring method returns a String consisting
    of the characters starting from the first
    position inclusive up to but NOT including the
    second position
  • String str "abcdefghijklmno"
  • String str2 str.substring( 2, 5 )
  • Result is "cde
  • Case changes toLowerCase( ), toUpperCase( )

8
Comparing Strings
  • A String is a reference type and so they are NOT
    compared with .
  • The String class has a method compareTo() to
    compare 2 strings.
  • The characters in each string are compared one at
    a time from left to right, using the collating
    sequence.
  • The comparison stops after a character comparison
    results in a mismatch, or one string ends before
    the other.
  • If str1 lt str2, then compareTo() returns an int lt
    0
  • If str1 gt str2, then compareTo() returns an int gt
    0
  • If the character at every index matches, and the
    strings are the same length, the method returns 0

9
Comparing Strings
  • What is the value of result for these examples?
  • Example 1
  • String str1 "abcde"
  • String str2 "abcfg"
  • int result str1.compareTo(str2)
  • Example 2
  • String str1 "abcde"
  • String str2 "ab"
  • int result str1.compareTo(str2)
Write a Comment
User Comments (0)
About PowerShow.com