javascript strings object

Upload: chandra-bhushan

Post on 03-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 JavaScript Strings Object

    1/21

    Strings Object , Substring Method &

    Regular Expression

    Presented by - Amit KumarChandra Bhushan

    JavaScript Presentation on

  • 7/28/2019 JavaScript Strings Object

    2/21

    JavaScript 2

    Objectives

    How to modify strings with common string method

    Creating dynamic effect with Substring method

    How to use the Regular expression with example

  • 7/28/2019 JavaScript Strings Object

    3/21

    JavaScript 3

    JavaScript String Object

    String Object

    A string is any text inside a quote pair

    A quote pair consists of either double or single quotes.

    This allows one string to nest inside another, as often happens withevent handlers.

    onclick = alert(Hello all)

    JavaScript imposes no practical limit on the number of

    characters that a string can hold.

    Most olderbrowser have a limit of 255 characters in length

    for a script statement.

    You may need to divide these lines into smaller chunks

  • 7/28/2019 JavaScript Strings Object

    4/21

    JavaScript 4

    JavaScript String Object

    You have two ways to assign a string value to a

    variable:

    var myString = Howdy;

    var myString = new String(Howdy); As of Navigator 3 and IE 4

    Joining Strings

    Bringing two strings together as a single string is called

    concatenation

    The addition operator (+) links multiple strings together

  • 7/28/2019 JavaScript Strings Object

    5/21

    JavaScript 5

    JavaScript String Object

    Concatenation Example:

    var msg = AMIT;

    msg = msg + KUMAR;msg = msg + SINGH;

    The pieces can be combinations of string

    literals (text inside quotes) or string variables.

  • 7/28/2019 JavaScript Strings Object

    6/21

    JavaScript 6

    JavaScript String Object

    The add-by-value operator (+=) provides ashortcut.

    var msg = AMIT;

    msg += KUMAR;msg += SINGH;

    You can also combine the operators if the need

    arises:var msg = AMIT;

    msg += KUMAR + SINGH;

  • 7/28/2019 JavaScript Strings Object

    7/21JavaScript 7

    JavaScript String Object

    String Methods The String object has the most diverse collection of

    methods associated with it (851-852).

    To use a string method, the string being acted upon

    becomes part of the reference followed by the methodname var result =string.methodName();

    Changing String Case Methods To convert to Uppercase

    var result =string.toUpperCase(); To convert to Lowercase

    var result =string.toLowerCase();

  • 7/28/2019 JavaScript Strings Object

    8/21JavaScript 8

    JavaScript String Object

    These methods come in handy when your

    scripts need to compare strings that may not

    have the same case.

    A string in a lookup table verses a string entered

    by a user

    Because the methods dont change the original

    strings attached to the expressions, you can simplycompare the evaluated results of the methods.

  • 7/28/2019 JavaScript Strings Object

    9/21JavaScript 9

    JavaScript String Object

    var foundMatch = false;stringA = "test";

    stringB = "TeST";

    var stringC = ; //Null string

    if (stringA.toUpperCase() = = stringB.toUpperCase())

    foundMatch = true;

    document.write(foundMatch);

    stringC=stringA.toUpperCase();

    document.write("
    " + stringC)

    Produces:

    true

    TEST

  • 7/28/2019 JavaScript Strings Object

    10/21JavaScript 10

    JavaScript String Object

    String Searches

    You can use the string.indexOf() method to see if

    one string is contained by another

    document.write(navigator.userAgent + "
    ");

    document.write(navigator.userAgent.indexOf("Windows"));

    Produces

    Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461; SV1; .NET CLR 1.1.4322)

    35

    If no match appears a value of -1 is returned.

    You can use this fact to test if a string is nested in another string

  • 7/28/2019 JavaScript Strings Object

    11/21JavaScript 11

    JavaScript String Object

    Extracting copies of character and substrings To extract or examine a single character at a know position within a string, use thecharAt() method

    var stringA = Building C;

    var bldgLetter = stringA.charAt(9);

    //results would be the letter C

    Another method, string.substring(), enables the extraction of a sequence of characters.

    NOTE: The character at the ending position is not included in the extracted string.

    var stringA = banana daiquiri;

    var excerpt = stringA.substring(2, 6); //6-2 = 4 characters extracted

    //result: excerpt = nana;

    The number 2 desiganted the straring position of the extracted string (index begins atzero)

    The number 6 designates the number of charters to extract

  • 7/28/2019 JavaScript Strings Object

    12/21

    Methods of the String Object

    JavaScript 12

    ccMethod DescriptioncharAt( index ) Returns a string containing the character at the specified index. If there is no

    character at the index, charAt returns an empty string. The first character islocated at index 0.

    charCodeAt( index ) Returns the Unicode value of the character at the specified index. If there isno character at the index, charCodeAt returns NaN (Not a Number).

    concat( string ) Concatenates its argument to the end of the string that invokes the method.

    The string invoking this method is not modified; instead a new String isreturned. This method is the same as adding two strings with the string

    concatenation operator+ (e.g., s1.concat(s2) is the same as s1+s2).fromCharCode(

    value1, value2, )Converts a list of Unicode values into a string containing the corresponding

    characters.indexOf(

    substring, index )Searches for the first occurrence ofsubstringstarting from position index in

    the string that invokes the method. The method returns the starting index ofsubstringin the source string or1 ifsubstringis not found. If the index

    argument is not provided, the method begins searching from index 0 in thesource string.

    lastIndexOf(substring, index )

    Searches for the last occurrence ofsubstringstarting from position index andsearching toward the beginning of the string that invokes the method. The

    method returns the starting index ofsubstringin the source string or1 if

    substringis not found. If the index argument is not provided, the method

    begins searching from the end of the source string.

  • 7/28/2019 JavaScript Strings Object

    13/21

    Methods of the String Object

    JavaScript 13

    Methods that generate

    XHTML tagsanchor( name ) Wraps the source string in an anchor element

    () with name as the anchor name.blink() Wraps the source string in a

    element.

    fixed() Wraps the source string in a element.

    link( url ) Wraps the source string in an anchor element() with urlas the hyperlink location.

    strike() Wraps the source string in a element.

    sub()Wraps the source string in a element.

    sup() Wraps the source string in a element.

  • 7/28/2019 JavaScript Strings Object

    14/21

    String.substring() Method

    The String.substring()method returns a sub-string of characters beginning at a specifiedindex and ending at a specified index. We

    supply the beginning and ending indexes asarguments, separated with a comma.

    While the method includes the beginning indexas part of the returned substring, it does NOTinclude the ending index.

    JavaScript 14

  • 7/28/2019 JavaScript Strings Object

    15/21

    JavaScript substring() Method

    The substring() method extracts the characters

    from a string, between two specified indices,

    and returns the new sub string.

    This method extracts the characters in a string

    between "from" and "to", not including "to"

    itself.

    Syntax

    string.substring(from, to)

    JavaScript 15

  • 7/28/2019 JavaScript Strings Object

    16/21

    JavaScript 16

    Example

    Extract characters from a string:

    var str="Hello world!";

    document.write(str.substring(3)+"
    ");

    document.write(str.substring(3,7));

    The output of the code above will be:

    lo world!lo w

  • 7/28/2019 JavaScript Strings Object

    17/21

    JavaScript Regular Expressions

    Regular expressions are patterns used to match

    character combinations in strings. In

    JavaScript, regular expressions are also

    objects. These patterns are used with

    the exec and test methods ofRegExp, and with

    the match, replace, search, and splitmethods

    ofString. This chapter describes JavaScriptregular expressions.

    JavaScript 17

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exechttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/testhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExphttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/matchhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replacehttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/searchhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/splithttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Stringhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Stringhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/splithttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/searchhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/searchhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replacehttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/matchhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExphttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExphttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/testhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec
  • 7/28/2019 JavaScript Strings Object

    18/21

    Creating a Regular Expression

    Syntax

    var patt=new RegExp(pattern,modifiers)

    or more simply:

    var patt=/pattern/modifiers;

    pattern specifies the pattern of an expression

    modifiers specify if a search should be global,

    case-sensitive, etc. JavaScript 18

  • 7/28/2019 JavaScript Strings Object

    19/21

    RegExp Object Methods

    1.compile()- Compiles a regular expression

    2.exec() -Tests for a match in a string.

    Returns the first match

    3.test() - Tests for a match in a string.

    Returns true or false

    JavaScript 19

  • 7/28/2019 JavaScript Strings Object

    20/21

    EXAMPLE OF REGULAR

    EXPRESSION

    var str = "Visit W3Schools"; var patt1 = /w3schools/i;

    document.write(str.match(patt1));

    output - W3Schools

    JavaScript 20

  • 7/28/2019 JavaScript Strings Object

    21/21

    JavaScript 21