Вы находитесь на странице: 1из 21

JavaScript Presentation on

Strings Object , Substring Method & Regular Expression

Presented by - Amit Kumar Chandra Bhushan

Objectives
How to modify strings with common string method
Creating dynamic effect with Substring method

How to use the Regular expression with example

JavaScript

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 with event handlers.
onclick = alert(Hello all)

JavaScript imposes no practical limit on the number of characters that a string can hold. Most older browser have a limit of 255 characters in length for a script statement.
You may need to divide these lines into smaller chunks

JavaScript

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

JavaScript

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.

JavaScript

JavaScript String Object


The add-by-value operator (+=) provides a shortcut.
var msg = AMIT; msg += KUMAR; msg += SINGH;

You can also combine the operators if the need arises:


var msg = AMIT; msg += KUMAR + SINGH;
JavaScript

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 method name
var result = string.methodName();

Changing String Case Methods


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

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

JavaScript

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 simply compare the evaluated results of the methods.

JavaScript

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("<br />" + stringC)

Produces: true TEST

JavaScript

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 + "<br />"); 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

JavaScript

10

JavaScript String Object


Extracting copies of character and substrings
To extract or examine a single character at a know position within a string, use the charAt() 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 at zero) The number 6 designates the number of charters to extract
JavaScript

11

Methods of the String Object


ccMethod charAt( index ) charCodeAt( index ) concat( string ) Description 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 is located at index 0. Returns the Unicode value of the character at the specified index. If there is no character at the index, charCodeAt returns NaN (Not a Number). 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 is returned. 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). Converts a list of Unicode values into a string containing the corresponding characters. Searches for the first occurrence of substring starting from position index in the string that invokes the method. The method returns the starting index of substring in the source string or 1 if substring is not found. If the index argument is not provided, the method begins searching from index 0 in the source string. Searches for the last occurrence of substring starting from position index and searching toward the beginning of the string that invokes the method. The method returns the starting index of substring in the source string or 1 if substring is not found. If the index argument is not provided, the method begins searching from the end of the source string.
JavaScript

fromCharCode( value1, value2, ) indexOf( substring, index )

lastIndexOf( substring, index )

12

Methods of the String Object


Methods that generate XHTML tags anchor( name ) Wraps the source string in an anchor element (<a></a>) with name as the anchor name. blink() Wraps the source string in a <blink></blink> element. fixed() Wraps the source string in a <tt></tt> element. link( url ) Wraps the source string in an anchor element (<a></a>) with url as the hyperlink location. strike() Wraps the source string in a <strike></strike> element. sub() Wraps the source string in a <sub></sub> element. sup() Wraps the source string in a <sup></sup> element.

JavaScript

13

String.substring() Method
The String.substring() method returns a substring of characters beginning at a specified index and ending at a specified index. We supply the beginning and ending indexes as arguments, separated with a comma. While the method includes the beginning index as part of the returned substring, it does NOT include the ending index.

JavaScript

14

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

Example Extract characters from a string: <script> var str="Hello world!"; document.write(str.substring(3)+"<br>"); document.write(str.substring(3,7)); </script> The output of the code above will be: lo world! lo w

JavaScript

16

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 of RegExp, and with the match, replace, search, and splitmethods of String. This chapter describes JavaScript regular expressions.

JavaScript

17

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

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

EXAMPLE OF REGULAR EXPRESSION


<!DOCTYPE html> <html> <body> <script> var str = "Visit W3Schools"; var patt1 = /w3schools/i; document.write(str.match(patt1)); </script> </body> </html> output W3Schools

JavaScript

20

JavaScript

21

Вам также может понравиться