Sunday, April 17, 2016

JavaScript String Methods

STRING:
JavaScript will store any text inside double quotes “” or single quotes ‘’.
Example: “My first Java Program” or ‘My first Java Program’.


JavaScript String Methods:
  1. indexOf()
The index of method will return the first occurrence of a particular text in a string.
Example: var a = "My name is Arun. My father's name is Ashok";
                    alert(a.indexOf('name'));
In the above example name is appearing twice. But it will take the position of the first time “name” and will return 3 .
Result: 3
  1. lastIndexOf()
The index of method will return the first occurrence of a particular text in a string.
Example: var a = "My name is Arun. My father's name is Ashok";
                   alert(a.lastIndexOf('name'));
In the above example name is appearing twice. But it will take the position of the last position of “name” and will return 29 .
Result: 29
Both the indexOf(), and the lastIndexOf() methods return -1 if the text is not found.
  1. toUpperCase()
It will convert the string in uppercase.
Example:  var a = "Live life to the fullest";
                   alert(a.toUpperCase());
Result: LIVE LIFE TO THE FULLEST
  1. toLowerCase()
It will convert the string in uppercase.
Example: var a = "Live life to the fullest";
                    alert(a.toLowerCase());
Result: live life to the fullest
  1. length
It will return length of the string.
Example: var a = "Live life to the fullest";
               alert(a.length);
It will count total letters in a string and blank spaces too and return total value of
it.
Result: 24
  1. charAt()
It will return character for the given position.
  Example: var a = "Live life to the fullest";
                              alert(a.charAt(2));
                 Result: v
For Invalid values:-
    
  Example: var a = "Live life to the fullest";
                     alert(a.charAt(26));
      Result: Null

  1. substr()
Syntax: substr(Begin Index, no. of values)
Begin index will take the position of the character in a string and will display values depending upon the index value and number given.
Example: var a = "Live life to the fullest";
                  alert(a.substr(2,6));
Result: ve lif

For Invalid values:-
Example: var a = "Live life to the fullest";
               alert(a.substr(27,6));
Result: Nothing

  1. substring()
Syntax: substr(Begin Index, End Index)
Begin index will take the position of the character in a string and will display values depending upon the index value and number given.
 Example: var a = "Live life to the fullest";
                             alert(a.substr(2,6));
                Result: ve lif
Example: var a = "Live life to the fullest";
               alert(a.substr(27,6));
               Result: Nothing

  1. Match()
It will match the value given with the string and return it.
Example: var a = "Live life to the fullest";
                alert(a.match(‘l’));
Result: l
For Invalid values:-
Example: var a = "Live life to the fullest";
               alert(a.match(‘z’));
Result: Null
  1. Replace()
It will replace a specified value with the given value in the string.
Example: var a = "Live life to the fullest";
               alert(a.replace(‘l’ , ‘m’));
Result: Mive mife to the fummest
Example: var a = "Live life to the fullest";
              alert(a.replace(‘o’ , ‘z’));
Result: Live life to the fullest
It will return the string as it is.
  1. Search()
This method will search the sting for the given value and returns the position of the value.
Example:  var a = "Live life to the fullest";
               alert(a.search(‘l’));
Result: 5, not 0 because JavaScript is case sensitive. If we will search L, then it will
return 0.Also it will include blank spaces while counting the position.

For Invalid values:-
Example:  var a = "Live life to the fullest";
               alert(a.search(‘q’));
Result: -1

REGULAR EXPRESSION
Regular expression is used to perform all types of text searches/replace operations on the basis of given modifiers..

Syntax:
/pattern/modifiers;

Different Types of Modifiers:
1. i : Using this modifier will do case sensitive matching
2. g : It will do global match which means it will find all the matches in a string rather than stopping after first match.
3. m : It will do multiline matching.

Example:
var str = 'admec multimedia institute';
alert(str.search(/MULTIMEDIA/i));
Result: 6, As we have used search method it will search for the given value and return the position of the given value. Also as we have used ‘i’ as the modifier it will do case sensitive matching.
Example:
var str = 'admec multimedia institute';
var patt = /A/gi;
alert(str.replace(patt, 'z'));
Result: zdmec multimediz institute, As we have used replace method and ‘g’ and ‘I’ modifier it will do case sensitive matching and replace all A with z.

Test() Method
It will search a string for a pattern and will return true or false value depending upon the search.
Example:
var str = 'admec multimedia institute3';
var patt = /[0-9]/gi;
      if(patt.test(str) == false)
         alert('valid name given');
       else{
         alert('invalid name given');
}
Result: invalid name given, it will search for values between[0-9]. If it’s there in the string it will execute the else statement and will print ‘invalid name given’.

No comments:

Post a Comment