Using Javascript string objects.

Using the build-in String object.

  • You can create an object with a member variable that is a primitive datatype string with this Javascript build-in String object.
  • You use the new keyword to create an object that contains a primitive string.
    var obj_num= new String("Hello");
  • This Javascript build-in String object is also a wrapper for all primitive strings.
  • This means you can use the properties and methods contained in the String object, not only on all new-created string objects, but also on all primitive strings.
    <script type="text/javascript">
      // create a primitive string
      var prim_str1 = "You can create primitive strings or String objects";
      document.write(prim_str1+"<br>");
      // The same creating an object with a primitive string
      var obj_str1 = new String("You can create primitive strings or String objects");
      document.write(obj_str1+"<br>");
    </script>
  • Javascript will automatically use the valueOf() method, which exits in the String object, when there is a need to convert a String object value to a primitive value.
    For example:
    <script type="text/javascript">
     // First a primitive number.
     var prim_str1="Javascript uses the valueOf() method" ;
     // Here is the same, using an object.
     var obj_str1= new String("Javascript uses the valueOf() method");
     document.write("Javascript uses the valueOf() method "+
                       " before comparing strings<br>");
     if (prim_str1==obj_str1){
       document.write("Strings are equal<br>");
     }
     </script>
  • As you probably already have seen there is no good reason to create string objects.
  • At last; You can use the String object as a function to create a primitive string.
  • Whether you create an object, or use the String object as a function, you can convert a strings with a number to a number object or a primitive number value.
    For example:
    <script type="text/javascript">
      // create a primitive string with numbers
      var prim_str1 = 6 +" hotdogs and "+ 2 + " pizza please!";
      document.write(prim_str1+"<br>");
      // The same; create an object with a primitive string with numbers
      var obj_str1 = new String(6 +" hotdogs and "+ 2 + " pizza please!");
      document.write(obj_str1+"<br>");
    </script>
  • A short notation for converting a single number to a string is:
    var prim_str1 = 6.461 +""
    // prim_str1 will then contains the string; '6.462' 

Most used string methods.

  • Concatenate strings

    You can concatenate strings in Javascript using the + operator:
    <script type="text/javascript">
    var message = "Hello, " + "mister";   // Produces the string "Hello, mister"
    document.write(message+"<br>");
    </script>
  • Length of a string

    You can find the length of a string (the number of characters it contains) using the length property of the string.
    <script type="text/javascript">
    var str1="This string has a lot of characters";
    document.write("The 'str1' string contains "+str1.length+" characters.<br>");
    </script>
  • Get a char from a string

    All characters in a string has an index starting with 0 as the first character and length-1 as the last character. You can get any character from the string using the charAt() method.
    <script type="text/javascript">
    var str1="This string has a lot of characters";
    document.write("The last character of the string is: "+
                    str1.charAt(str1.length - 1)+"<br>");
    </script>
  • Index of a char in a string

    All characters in a string has an index starting with 0 as the first character and length-1 as the last character. You can get any index of string in a string using the indexOf() method.
    <script type="text/javascript">
      var str1="This string has a lot of characters";
      document.write("The string 'lot' starts at index: "+
        str1.indexOf("lot")+" in the string:<br>");
      document.write(str1+"<br>");
    </script>
  • Get a string from a string

    You can get any string from the string using the substring() method or the substr() method.
    <script type="text/javascript">
      var str1="This string has a lot of characters";
      // using the substring(from, to);
      document.write("String from index 18 up to index 21 is: '"+
        str1.substring(18, 21)+"' in the string:<br>");
      document.write(str1+"<br>");
       // using the substr(from, length);
      document.write("String from index 18 with length 3 is: '"+
        str1.substr(18, 3)+"' in the string:<br>");
      document.write(str1+"<br>");
    </script>
  • Replace a string with a string in a string

    You replase any string with a string in a string using the replace() method.
  • This method has two parameters (regexp and replacement):
    1. If the regexp argument is a string, it is used as a literal text pattern to be searched for in the string. Else you can specify a RegExp pattern to be searched for in the string.
    2. The second argument, replacement, is the string for replacement.
    <script type="text/javascript">
      var str1="This#string#has#a#lot#of#characters";
      document.write(str1+"<br>");
      document.write("Now we replace all # with a space character in this string:<br>");
      document.write(str1.replace(/#/g," ")+"<br>");
    </script>

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.