Properties and ...LOGIN

Properties and methods of String object in JavaScript

Properties and methods of String object

  • ## length: Get the string length. For example: var len = strObj.length

  • toLowerCase(): Convert the letters in the string to all lowercase. For example: strObj.toLowerCase()

  • toUpperCase(): Convert the letters in the string to all uppercase. Such as: strObj.toUpperCase()

  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
               var str="ASDfgh";
               document.write(str.length+"<br/>");
               document.write(str.toLowerCase()+"<br/>");
               document.write(str.toUpperCase()+"<br/>");
            </script>
        </head>
        <body>
        </body>
    </html>


##charAt(index)

    Function: Returns a character at the specified subscript position. If not found, an empty string is returned.
  • Syntax: strObj.charAt(index)
  • Parameters: index is a specified index number, and a character is returned based on the index index number .
  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
               var str="Hello JavaScript";
                for(var i=0;i<str.length;i++){
                    document.write(str.charAt(i)+"<br/>");
                }
            </script>
        </head>
        <body>
        </body>
    </html>


##indexOf()


Function: Return the index value of a substring in the original string (the search order is from left to right). If not found, -1 is returned.
  • Syntax: strObj.indexOf(substr)
  • Parameters: substr represents a substring to be found.
  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
               var str="Hello JavaScript";
               document.write(str.indexOf("J")+"<br/>");   
               document.write(str.indexOf("S")+"<br/>");   
               document.write(str.indexOf("s")+"<br/>");   
            </script>
        </head>
        <body>
        </body>
    </html>


lastIndexOf()


Function : In the original string, search for a certain substring from right to left. If not found, -1 is returned.
  • Syntax: strObj.lastIndexOf(substr)
  • Parameters: substr represents the substring to be found.
  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
               var str="Hello JavaScript";
               document.write(str.lastIndexOf("J")+"<br/>");   
               document.write(str.lastIndexOf("S")+"<br/>");   
               document.write(str.lastIndexOf("s")+"<br/>");   
            </script>
        </head>
        <body>
        </body>
    </html>

##substr()


Function: In the original string, return a substring.

  • Syntax: strObj.substr(startIndex [ , length])

  • Description: Return length characters starting from the subscript startIndex.

  • Parameters:

  • startIndex represents the starting index number of the search;
  • length is optional, how many characters are returned. If omitted, always returns to the end.

  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
               var str="Hello JavaScript";
               document.write(str.substr(6)+"<br/>");
               document.write(str.substr(6,4)+"<br/>");               
            </script>
        </head>
        <body>
        </body>
    </html>

##substring()


Function: In the original string, return a substring.

  • Syntax: strObj.substring(startIndex [, endIndex])

  • Description: Returns the starting index value from startIndex to the end index value of endIndex all characters between.

  • parameter:

  • startIndex: represents the start index value.

  • endIndex: optional, representing the end index value. If omitted, all characters up to the end are generally returned.

Note: If the second parameter is omitted, the results of substr() and substring() are the same.

Note: The characters returned by substring() include the characters at startIndex and do not include the characters at endIndex.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
           var str="Hello JavaScript";
           document.write(str.substring(6)+"<br/>");
           document.write(str.substring(6,10)+"<br/>");               
        </script>
    </head>
    <body>
    </body>
</html>


split(): Convert a string into an array

  • Function: Cut a string into several segments. Returns an array.

  • Syntax: strObj.split (split number)

  • Parameters: The parameter is a string of split numbers. Cut the string into segments using the specified split number.

Use a space as the dividing number to divide the string Hello JavaScript
into an array Hello, JavaScript ​ ​

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
           var str="Hello JavaScript";
           document.write(str.split(" "))           
        </script>
    </head>
    <body>
    </body>
</html>



Next Section
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var str="ASDfgh"; document.write(str.length+"<br/>"); document.write(str.toLowerCase()+"<br/>"); document.write(str.toUpperCase()+"<br/>"); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware