Thanks to visit codestin.com
Credit goes to www.slideshare.net

Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
 String Length
 The length property returns the length of a string:
 Example
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
 Output: The length property returns the length of a string:
26
 Finding a String in a String
 The indexOf() method returns the index of (the position of)
the first occurrence of a specified text in a string:
 Example
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");
 Output: The indexOf() method returns the position of the first
occurrence of a specified text:
7
 The lastIndexOf() method returns the index of
the last occurrence of a specified text in a string:
Example
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate");
 Output:The lastIndexOf() method returns the position of the last
occurrence of a specified text:21
 Both indexOf(), and lastIndexOf() return -1 if the text is not
found.
Example
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("John");
 Output: -1
 Both methods accept a second parameter as the starting position
for the search:
 The search() method searches a string for a specified value and
returns the position of the match:
Example
var str = "Please locate where 'locate' occurs!";
var pos = str.search("locate");
 Output: The search() method returns the position of the first
occurrence of a specified text in a string: 8
There are 3 methods for extracting a part of a string:
 slice(start, end)
 substring(start, end)
 substr(start, length)
 slice() extracts a part of a string and returns the extracted part in a
new string.
 The method takes 2 parameters: the start position, and the end
position (end not included).
 This example slices out a portion of a string from position 7 to
position 12 (13-1):
Example
var str = "Apple, Banana, Kiwi";
var res = str.slice(7, 13);
 The result of res will be: Banana
 substring() is similar to slice().
 The difference is that substring() cannot accept negative indexes.
Example
var str = "Apple, Banana, Kiwi";
var res = str.substring(7, 13);
 The result of res will be: Banana
 substr() is similar to slice().
 The difference is that the second parameter specifies
the length of the extracted part.
Example
var str = "Apple, Banana, Kiwi";
var res = str.substr(7, 6);
 The result of res will be: Banana
 A string is converted to upper case with toUpperCase():
Example
var text1 = "Hello World!";
var text2 = text1.toUpperCase();
Output: HELLO WORLD!
 A string is converted to lower case with toLowerCase():
Example
var text1 = "Hello World!";
var text2 = text1.toLowerCase();
Output: hello world!

Javascript string method

  • 1.
    Prof. Neeraj Bhargava KapilChauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2.
     String Length The length property returns the length of a string:  Example var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var sln = txt.length;  Output: The length property returns the length of a string: 26  Finding a String in a String  The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:  Example var str = "Please locate where 'locate' occurs!"; var pos = str.indexOf("locate");  Output: The indexOf() method returns the position of the first occurrence of a specified text: 7
  • 3.
     The lastIndexOf()method returns the index of the last occurrence of a specified text in a string: Example var str = "Please locate where 'locate' occurs!"; var pos = str.lastIndexOf("locate");  Output:The lastIndexOf() method returns the position of the last occurrence of a specified text:21  Both indexOf(), and lastIndexOf() return -1 if the text is not found.
  • 4.
    Example var str ="Please locate where 'locate' occurs!"; var pos = str.lastIndexOf("John");  Output: -1  Both methods accept a second parameter as the starting position for the search:
  • 5.
     The search()method searches a string for a specified value and returns the position of the match: Example var str = "Please locate where 'locate' occurs!"; var pos = str.search("locate");  Output: The search() method returns the position of the first occurrence of a specified text in a string: 8
  • 6.
    There are 3methods for extracting a part of a string:  slice(start, end)  substring(start, end)  substr(start, length)
  • 7.
     slice() extractsa part of a string and returns the extracted part in a new string.  The method takes 2 parameters: the start position, and the end position (end not included).  This example slices out a portion of a string from position 7 to position 12 (13-1): Example var str = "Apple, Banana, Kiwi"; var res = str.slice(7, 13);  The result of res will be: Banana
  • 8.
     substring() issimilar to slice().  The difference is that substring() cannot accept negative indexes. Example var str = "Apple, Banana, Kiwi"; var res = str.substring(7, 13);  The result of res will be: Banana
  • 9.
     substr() issimilar to slice().  The difference is that the second parameter specifies the length of the extracted part. Example var str = "Apple, Banana, Kiwi"; var res = str.substr(7, 6);  The result of res will be: Banana
  • 10.
     A stringis converted to upper case with toUpperCase(): Example var text1 = "Hello World!"; var text2 = text1.toUpperCase(); Output: HELLO WORLD!  A string is converted to lower case with toLowerCase(): Example var text1 = "Hello World!"; var text2 = text1.toLowerCase(); Output: hello world!