String - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

String

Description:

String str4 = 'Day of the seize'; String str5 = 'Seize the day' ... System.out.println('str5 is now: ' str5); //str5 is now: SXizX thX day //now check again ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 7
Provided by: uCsB7
Category:
Tags: string | thx

less

Transcript and Presenter's Notes

Title: String


1
  • String
  • StringBuffer

2
  • class StringExample
  • public static void main (String args)
  • String str1 "Seize the day"
  • String str2 new String()
  • String str3 new String(str1)
  • String str4 "Day of the seize"
  • String str5 "Seize the day"
  • System.out.println("str1 " str1)
    // str1 Seize the day
  • System.out.println("str2 " str2)
    // str2
  • System.out.println("str3 " str3)
    // str3 Seize the day
  • System.out.println("str4 " str4)
    // str4 Day of the seize
  • System.out.println("str5 " str5)
    // str5 Seize the day
  • System.out.println()
  • System.out.println("length of str1 is "
    str1.length()) //length of str1 is 13
  • System.out.println("length of str2 is "
    str2.length()) //length of str2 is 0
  • System.out.println()

3
  • System.out.println("Index of 'e' in str4
    "
  • str4.indexOf('e'))
    // Index of 'e' in str4 9
  • System.out.println("Char at pos 3 in
    str1 "
  • str1.charAt(3))
    //Char at pos 3 in str1 z
  • System.out.println("Substring 6 to 8 of
    str1 "
  • str1.substring(6,8))
    // Substring 6 to 8 of str1 th
  • if (str1str5)
  • System.out.println("str1 and str5
    refer to the
  • same object")
    //str1 and str5 refer to the same object
  • if (str1 ! str3)
  • System.out.println("str1 and str3
    don't refer to
  • the same
    object") //str1 and str3 don't refer to the same
    object
  • if (str1.equals(str3))
  • System.out.println("str1 and str3
    contain the
  • same chars")
    //str1 and str3 contain the same chars
  • System.out.println()

4
  • str2 str1.toUpperCase()
  • System.out.println("str2 now is "
    str2) //str2 now is SEIZE THE DAY
  • str5 str1.replace('e','X')
  • System.out.println("str5 is now "
    str5) //str5 is now SXizX thX day
  • //now check again
  • if (str1str5)
  • System.out.println("str1 and str5
    refer to the
  • same object")
  • else System.out.println("str1 and str5
    don't refer to
  • the same
    object") //str1 and str5 don't refer to the same
    object

5
  • Useful methods for string
  • length() returns the length
  • charAt (int index) returns char at that
    positions (0..)
  • indexOf(char ch) returns index (0..) of
    first occurrence
  • lastindexOf(char ch) returns index (0..)
    of last occurrence
  • endsWith(String suffix) returns true if has this
    suffix
  • startsWith(String prefix) returns true if has
    this prefix
  • equals(Object obj) returns true if two
    strings are the same
  • equalsIgnoreCase(Object obj) returns true if two
    strings are equal, ignoring case
  • toLowerCase() returns a new string of
    lower case
  • toUpperCase() returns a new string of
    upper case
  • substring(int begin, int end) returns a new
    string that is a substring of this string
    including begin, excluding end.

6
  • java.lang.StringBuffer.
  • StringBuffer- implements a mutable sequence of
    characters.
  • String - implements a constant sequence of
    characters.
  • public class ReverseString
  • public static void main(String args)
  • String source"abcdefg"
  • int strLensource.length()
  • StringBuffer dest new StringBuffer( strLen
    )
  • for( int i strLen-1 igt0 i--)
  • dest.append( source.charAt(i) )
  • System.out.println( dest )
  • output gfedcba
Write a Comment
User Comments (0)
About PowerShow.com