Common Methods of string
Method |
Description |
Anchor () |
Create an HTML anchor |
Concat () |
Connect strings |
Indexof () |
Find the location where the character appears |
Lastindexof () |
Find the last character |
Charat () |
Returns the characters at the specified position. |
Substring () |
Truncate string |
Substr () |
Truncate string |
Spilt () |
Split string |
Tolowercase () |
Converts a string to lowercase letters. |
Touppercase () |
Converts a string to uppercase or lowercase letters. |
Sub () |
Returns the string as a subscript. |
Sup () |
Display the string as a supermark |
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
/************ Create a String object ***********/
// Create a String object (method 1)
// Var STR = "Hello World ";
// Create a String object (method 1)
// Var STR = new string ("Hello World ");
/*********** Common methods for string objects ***************/
VaR STR = "Hello World ";
// Indexof is used to find the index location of the first occurrence of the substring. If no index is found,-1 is returned.
// Document. Write (Str. indexof ("O", 0); // output 4
// Document. Write (Str. indexof ("O", 5); // output 7
// Lastindexof is used to find the final position of the specified sub-string. If no result is found,-1 is returned. (Note: This method is used to search from the front of the string, the second parameter indicates the index location to start searching)
// Document. Write (Str. lastindexof ('O', str. Length ));
/********* Substring and substr *********************/
VaR STR = "Hello World ";
// Parameter 1: subscripts to be intercepted. Parameter 2: the number of characters to be truncated.
Document. Write (Str. substring (1, 3); // output el
Document. Write ("<br> ");
// Parameter 1: subscripts to be captured; parameter 2: number of characters to be truncated
Document. Write (Str. substr (1, 3 ));
Document. Write ("<br> ");
Document. Write (Str. Length); // The length of the output string
</SCRIPT>
</Head>
<Body>
</Body>
</Html>