Practical No.5: Develop JavaScript to implement strings.

 

What is String?

  String is a collection of characters.

  Commonly used method of string object are concatenating two strings, converting

the string to upper case or lower case, finding the substring of a given string.

   String written within the single or double quotes.

Manipulating a String:

·       Manipulating string values is a common developer chore. This ranges from extracting portions of a string to determining if a string contains a specific character. The following JavaScript functions provide developers with everything they need:

 

1.      concat() - Combines the text of two or more strings and returns a new string.

2.      indexOf() Returns the starting index of a substring within another string. A –1 is returned if no match is found.

3.      charAt() Returns the character at the specified location.

4.      lastIndexOf() - Returns the index within the string of the last occurrence of the specified value, or -1 if not found.

5.      match() - Used to match a regular expression against a string.

6.      substring() A portion of a string is returned. A starting and ending location are passed to this function.

7.      replace() Used to find a match between a regular expression and a string, andto replace the matched substring with a new substring.

8.     search() - Executes the search for a match of a regular expression. If successful, search returns the index of the match inside the string. Otherwise, it returns -1.

9.      slice() - Extracts a section of a string and returns a new string.

10.   split() - Splits a string into an array of strings by separating the string intosubstrings.

11.    length() The length of the string is returned as the count of the number of characters it contains.

12.   toLowerCase() Converts the entire string to lowercase.

13.   toUpperCase() Converts the entire string to uppercase.

 

Joining A String:

  This method adds two or more strings and returns a new single string.   Syntax:               string.concat(string2, string3[, ..., stringN]);

  string2...stringN These are the strings to be concatenated.

Retrieving A Character From Given Position:


charAt() is a method that returns the character from the specified index.


  Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string, called stringName, is stringName.length – 1.

  Syntax:         string.charAt(index);

  index An integer between 0 and 1 less than the length of the string.

Retrieving A Position Of Character In A String:

  JavaScript array indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

  Syntax:                     string. indexOf(string); Dividing Text:

  This method splits a String object into an array of strings by separating the string into substrings.

  Syntax:                     string.split([separator][, limit]);

  separator Specifies the character to use for separating the string. If separator is omitted, the array returned contains one element consisting of the entirestring.

  limit Integer specifying a limit on the number of splits to be found.

Copying a Substring (Using of substr):

  This method returns the characters in a string beginning at the specified location through the specified number of characters.

  Syntax:         string.substr(start[, length]);

  start Location at which to start extracting characters (an integer between 0 and one less than the length of the string).

  length The number of characters to extract.

Copying a Substring (Using of substring):

  This method returns a subset of a String object.   Syntax: string.substring(indexA, [indexB])

  indexA An integer between 0 and one less than the length of the string.


indexB − (optional) An integer between 0 and the length of the string.


Converting String To Number:

  Converting string to number we have different types of function based on type of numbers i.e. string is to be converted to integer by using parseInt( ) method.

  If string is to be converted to float value then the method parseFloat().

Converting Number To String:

  This method returns a string representing the specified object.   Syntax:                           string.toString( )

  Returns a string representing the specified object.

Changing The Case Of String:

  This method returns the calling string value converted to lowercase.  Syntax: 

 string.toLowerCase( )

  This method returns the calling string value converted to uppercase.  Syntax:        

string.toUpperCase( )

 

Conclusion: We understand that how to implement string using various functions JavaScript.


JavaScript to implement string.

 

A]

<html>

<body>

<script>

function func()

{

var s = 'It';

var value = s.concat(' is',' a',' great',' day.'); document.write("String after concat: "+value);

var str = new String( "MVP RSM Polytechnic Nashik."); document.writeln("<br />String after str.charAt(4) is: " + str.charAt(4)); document.writeln("<br />String after str.charAt(5) is: " + str.charAt(5)); var index = str.indexOf( "Knowledge" );

document.write("<br />String after indexOf : " + index ); var splitted = str.split(" ", 2);

document.write( "<br />String after split : " + splitted ); document.write("<br />String after substr : " + str.substr(0,21)); document.write("<br />String after substring: " + str.substring(0, 20)); document.write("<br />String after toLowerCase: "+str.toLowerCase( )); document.write("<br />String after toUpperCase: "+str.toUpperCase( ));

}

func();

</script>

</body>

</html>

 

Output:

String after concat: It is a great day. String after str.charAt(4) is: B String after str.charAt(5) is: h String after indexOf : 12


B]

<html>

<body>

<script>

function func(){

        var s ='Kya,';

var value = s.concat(' Baat', ' Hai'); document.write("String after concat: " + value);

var str = new String("Kudi kehndi pehle jaguar lelo."); document.writeln("<br />String after str.charAt(4) is: " + str.charAt(4)); document.writeln("<br />String after str.charAt(5) is: " + str.charAt(5)); var index = str.indexOf("Knowledge");

document.write("<br />String after indexOf : " + index); var splitted = str.split(" ", 2);

document.write("<br />String after split : " + splitted); document.write("<br />String after substr : " + str.substr(0, 21)); document.write("<br />String after substring: " + str.substring(0, 20)); document.write("<br />String after toLowerCase: " + str.toLowerCase()); document.write("<br />String after toUpperCase: " + str.toUpperCase());

}

func();

</script>

</body>

</html>

Output:

String after concat: Kya, Baat Hai String after str.charAt(4) is: String after str.charAt(5) is: k String after indexOf : -1

String after split : Kudi,kehndi String after substr : Kudi kehndi pehle jag

String after substring: Kudi kehndi pehle ja

String after toLowerCase: kudi kehndi pehle jaguar lelo.

String after toUpperCase: KUDI KEHNDI PEHLE JAGUAR LELO.

Comments