Strings - PowerPoint PPT Presentation

About This Presentation
Title:

Strings

Description:

... is of length len and begins at the index toffset ... { String searchMe = 'Green Eggs and Ham'; String findMe = 'Eggs' ... The output from this program is Eggs. ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 13
Provided by: RMK
Category:
Tags: and | eggs | ham | strings

less

Transcript and Presenter's Notes

Title: Strings


1
Strings
  • Can view as array of chartacters
  • Implemented as a class with its own methods
  • Simplest operation on strings is catenating two
    strings
  • String x Good
  • String y Morning!
  • String z x y
  • String w x y // also works
  • Possible to concatenate strings to variables of
    other types e.g., int, float, etc.
  • Internally converted to string

2
Methods of string class
  • Determining the length of a string
  • String x abc
  • int lengthOfx x.length()
  • Determining the character at a position
  • String x abc
  • char cAt2 x.charAt(2)
  • char first x.charAt(0)
  • char last x.charAt(x.length()-1)
  • Case conversion
  • String x Mainak
  • String y x.toUpperCase() // y is MAINAK
  • String z x.toLowerCase() // z is mainak

3
More on concatenation
  • Concatenating strings is different from
    concatenating characters
  • String x a
  • String y b
  • String z x y // z is ab
  • char x1 a
  • char y1 b
  • char z1 x1 y1 // z is not ab
  • For concatenating two strings you can use the
    concat method also
  • String z to.concat(get).concat(her)

4
Extracting substrings
  • Can extract the substring starting at a position
  • String x together
  • String y x.substring(2) // y is gether
  • String z x.substring(3) // z is ether
  • String w x.substring(0) // same as x
  • String u x.substring(x.length()-1) // r
  • String v x.substring(x.length()) // blank
  • Can extract substring between two positions
  • String y x.substring(0, 5) // y is toget
  • String z x.substring(5, x.length()) // her
  • String w x.substring(5, 6) // w is h
  • String u x.substring(5, 5) // u is

5
Searching in a string
  • Finding the first occurrence of a character or a
    substring within a string
  • String x abracadabra
  • int k x.indexOf(a) // k is 0
  • int p x.indexOf(a, 1) // p is 3 search
  • // begins from pos 1
  • int t x.indexOf(e) // t is -1
  • int q x.indexOf(ra) // q is 2
  • int s x.indexOf(ra, 3) // s is 9
  • Possible to find the last occurrence also
  • int p x.lastIndexOf(r) // p is 9

6
Comparison of strings
  • Compares strings in dictionary order (also known
    as lexicographical order)
  • Returns zero if strings are equal
  • String x abc
  • String y abcd
  • String z ab
  • String w abd
  • int p x.compareTo(y) // p is negative
  • int q x.compareTo(x) // q is zero
  • int r x.compareTo(z) // r is positive
  • int s y.compareTo(w) // s is negative
  • This comparison is case sensitive
  • Use compareToIgnoreCase otherwise

7
Some more useful methods
  • Removing leading and trailing whitespaces
  • String x abc
  • String y x.trim() // y is abc
  • Test for prefix
  • String x Mainak
  • boolean y x.startsWith(Main) // y is true
  • boolean z x.startsWith(nak, 3) // z is true
  • Test for suffix
  • String x Canada
  • boolean y x.endsWith(ada) // y is true

8
Some more useful methods
  • Substitute all occurrences of a character with
    another character
  • String x deer
  • String y x.replace(e, o) // y is door
  • String z x.replace(a, o) // z is deer
  • Partial string match
  • String x abracadabra
  • boolean y x.regionMatches(true, 2, bracket,
    1, 3) // y is true
  • The first argument should be set to true if the
    match is intended to be case ignorant

9
regionMatches
  • boolean regionMatches(int toffset, String other,
    int ooffset, int len)
  • Tests whether the specified region of this string
    matches the specified region of the String
    argument.
  • Region is of length len and begins at the index
    toffset for this string and ooffset for the other
    string.

10
regionMatches
  • boolean regionMatches(boolean ignoreCase, int
    toffset, String other, int ooffset, int len)
  • Tests whether the specified region of this string
    matches the specified region of the String
    argument. Region is of length len and begins at
    the index toffset for this string and ooffset for
    the other string.
  • The boolean argument indicates whether case
    should be ignored if true, case is ignored when
    comparing characters.

11
RegionMatchesDemo
  • public class RegionMatchesDemo
  • public static void main(String args)
  • String searchMe "Green Eggs and Ham"
  • String findMe "Eggs"
  • int searchMeLength searchMe.length()
  • int findMeLength findMe.length()
  • boolean foundIt false
  • for (int i 0 i lt (searchMeLength -
    findMeLength) i)
  • if (searchMe.regionMatches(i, findMe, 0,
    findMeLength))
  • foundIt true
  • System.out.println(searchMe.substring(i,
    i findMeLength))
  • break
  • if (!foundIt) System.out.println("No match
    found.")

12
  • The program steps through the string referred to
    by searchMe one character at a time. For each
    character, the program calls the regionMatches
    method to determine whether the substring
    beginning with the current character matches the
    string the program is looking for.
  • The output from this program is Eggs.
Write a Comment
User Comments (0)
About PowerShow.com