String Manipulation - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

String Manipulation

Description:

Splits a string into an array of substrings based on the location and ... The split() Method ... 'apple, banana, peach, orange' What about 'apple, 0.99, banana, 0.50, peach, ... – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 27
Provided by: drrober4
Category:

less

Transcript and Presenter's Notes

Title: String Manipulation


1
Chapter 8
  • String Manipulation

2
Background
  • We already looked at the String object (Chapter
    4)
  • length
  • charAt()
  • indexOf()
  • substr() and substring()
  • toUpperCase() and toLowerCase()
  • Now well look at some very powerful other
    methods
  • split()
  • match()
  • replace()
  • search()

3
The split() Method
  • Splits a string into an array of substrings based
    on the location and existence of separators
  • Separation parameter
  • var myString A,B,C
  • var myTextArray myString.split(,)
  • ch8_examp1.htm Example

4
The replace() Method
  • Used to search for a substring within a string
    and where it finds it, it replaces it with the
    replacement substring specified.
  • var myString The event will occur in May, the
    21st of May
  • myCleanedupString myString.replace(May,June
    )
  • The event will occur in June, the 21st of June

5
The search() Method
  • Allows you to search a string for a particular
    piece of text.
  • If found, the character position is returned. If
    not found, a 1 is returned

6
The match() Method
  • Returns an array of all matches resulting from
    searching for a string within some text
  • Most useful when used with regular expressions

7
Regular Expressions
  • Provide a means of defining a pattern of
    characters, which are used to split, search, or
    replace characters in a string where they fit the
    defined pattern
  • RegExp object
  • A simple example

8
Regular Expressions Special Characters
9
Regular Expressions Special Characters
10
Regular Expressions Special Characters
11
Regular Expressions Special Characters
  • ch8_examp2.htm Example
  • Checks to see if a string you enter contains only
    letters and numbers
  • RegExp is an Object and has Methods
  • .test

12
Regular Expressions Repetition Characters
13
Regular Expressions Repetition Characters
14
Regular Expressions Repetition Characters
  • Examples
  • \d-\d\d\d-\d\d\d\d
  • Can be rewritten as
  • \d-\d3-\d4
  • Paula? Matches Paul or Paula

15
Regular Expressions Position Characters
16
Regular Expressions Position Characters
17
Regular Expressions Position Characters
  • Examples
  • myPattern
  • Matches an occurrence of myPattern if it is at
    the beginning of a line.
  • myPattern
  • Matches an occurrence of myPattern if it is at
    the end of a line.
  • Boundaries
  • boundaries.htm

18
Integrating
  • Complex string replacement
  • Paul, Paula, Paulina, paul, Paul
  • Want this to convert to
  • Ringo, Paula, Paulina, Ringo, Ringo
  • var myRegExp /Paul/gi
  • Ringo, Ringoa, Ringoina, Ringo, Ringo
  • var myRegExp /Paul\W/gi
  • \W matches any non-word character so this regular
    expression matches occurrences of Paul followed
    by a non-word character.
  • Ringo Paula, Paulina, Ringo Paul
  • Note the missing commas and final occurrence of
    Paul

19
Integrating
  • One more try using boundaries
  • var myRegExp /Paul\b/gi
  • Ringo, Paula, Paulina, Ringo, Ringo
  • Add another wrinkle
  • Paul, Paula, Paulina, paul, Paul, JeanPaul
  • var myRegExp /\bPaul\b/gi

20
Grouping Regular Expressions
  • Use () to group characters into patterns that we
    then apply repetition characters to
  • grouping regular expressions.htm
  • var myString "JavaScript, VBScript and Perl"
  • var myRegExp /\b(VB)?(Java)?Script\b/gi

21
Grouping Regular Expressions
  • Matching repeating characters
  • A small hypothetical situation
  • 009,007,001,002,004,003 is Valid
  • 007,007,001,002,002,003 is not
  • \d
  • \d,
  • \d,700
  • (\d),\1
  • repeating character.htm

22
The split() Method
  • Allows us to split a string into various pieces
    with the split being made at the character or
    characters specified as a parameter
  • Simple
  • apple, banana, peach, orange
  • What about
  • apple, 0.99, banana, 0.50, peach, 0.25, orange,
    0.75
  • Split and retrieve just the names of the fruits
  • a-z
  • ch8_examp3.htm example

23
The replace() Method
  • Inserting into a string using replace()
  • 1999, 2000, 2001
  • To
  • the year 1999, the year 2000, the year 2002
  • We can refer to groups of character using n
    where n refers to the position of the group
  • var myRegExp /\d4/g This finds each group
    of 4 digits
  • mystring.replace(myRegExp, the year 1)

24
The replace() Method
  • Another example
  • Replace single quotes with double quotes
  • Distinguish single quotes from apostrophe
  • Hello World said Mr. OConnerly.
  • He then said My Name is OConnerly, yes thats
    right, OConnerly
  • // will not work
  • /\B\B/g will
  • ch8_examp4.htm

25
The search() Method
  • Allows you to search a string for a pattern of
    characters. If found, return is the position at
    which it was found, else it returns 1
  • Finds first occurrence, the /g directive is
    irrelevant
  • ch8_examp5.htm

26
The match() Method
  • Similar to search except it returns an array with
    the matches
  • match example 1.htm
  • The years were 1999, 2000, and 2001
  • Extract just the years
  • myRegExp /\bd4\b/g
  • A complex example
  • ch8_examp5.htm
Write a Comment
User Comments (0)
About PowerShow.com