javaScript String object

the javascript string object allows manipulation and formatting of text strings and determination and location of substrings within strings.

strings are useful for holding data that can be represented in text form.the string literal value enclosed in single(') or double(") quotes.


         var    newString   =   new  string (  "stringliteral"  );

stringliteral :any group of unicode characters.

              var     name     =   new   string("javascript string") ;
              var     lastname =  new   string("object") ;
              var     salary   =    new   string('2303445') ;
document.write("name  ="+name+"<br/>"+"lastname  ="+lastname+

                                                       "<br/>"+"salary  ="+salary);

you can use quotes inside a string, as long as they don't match the quotes surrounding the string:


      var     string_design  =   'its "good for you you" think';
      var     string_1         =   "this isn't 'good' for you";
      var     string_2         =   "this is" wrong string";     //string value generate an error

you can put quotes inside a string by using the \ escape character.

                  var    name      =  "java\" javascript\"  string object" ;
                  var    salary    =  '200\' 333\' 444\' ';

string lenght property

the length property contains an integer that indicates the number of characters in the string object.

javaScript strings are immutable, so the length of a string cannot be modified.

                 var    salary = "234545455656576";
                 var    name   = "this is the javascript string object";

extract substring

the charAt() method returns a character value equal to the character at the specified index.the first character in a string is at index 0, and second is at index 1, and so forth.

the JavaScript String object charCodeAt() method returns the unicode of the character at the specified index

            var  name   =   "javascript objects";
            document.write("3rd index character = "+name.charat(0));
            document.write("<br/>"+"5th index character = "+name.charat(5));
document.write("<br/>"+"last index char = "+name.charat(name.length-1));
document.write("<br/>"+"unicode value of 3rd char = "+name.charcodeat(3));

the substring() method extracts the characters in a string between "start" and "end" position, not including "end" itself.

         var   value  ="this is khan , what you want this is not a matter";
         var   substring = value.substring(10,40);

string uppercase and lowercase

a touppercase() method conerts all the alphabetic characters in a string to uppercase.

a tolower case() method conerts all the alphabetic characters in a string to lowercase.

the toUpperCase/toLowerCase method has no effect on non-alphabetic characters.

      var     name  =  new  string("sandeep kumar nehra");
      document.write("upper case____"+name.toUpperCase());
document.write('<br/>'+"lower case____"+name.toUpperCase().toLowerCase());

string replace/concat methods

the replace() method replaces a specified value with another value in a string.

the concat() method returns a string that contains the concatenation of two or more string.

       var   demo  = "this is exchange offer";
       var   result = demo.replace("exchange","free");
       document.write('<br/>'+result.concat("this is exchange offer"));

the javascript string trim() removes the leading and trailing white space and line terminator characters from a string

                  var   string   ="     this is ram      \n";
                  var   length   =string.length;
                  document.write("before trim the string length _____"+length);
                  var   length   =string.trim().length;
document.write("<br>"+"after trim() method call the string length____"+length);

finding string

the indexof() method returns the position of the first found occurrence of a specified text inside a string:

the lastindexof() method returns the position of the last found occurrence of a specified value in a string

note :this method returns -1 if the specified text is not found

     var     name  ="bill gates melinda gates steve jobes";

                    var     position = name.indexof('gates');
                    var   last_posi  = name.lastindexof('gates');
               document.write("the index of position___"+position);
               document.write("<br/>"+"the last index of position___"+last_posi);

matching content

the match() matches a string with a regular expression, and returns an array containing the result of that search.

if the match method does not find a match, it returns null.

     var     value  = "this is good guy";