JavaScript's String method Rollup _ Basics

Source: Internet
Author: User
Tags join

1. Stringobject.charat ()

Function: Returns the subscript of a string

var str= "This is a string of strings";
Console.log (Str.charat (0))//This

2. Stringobject.charcodeat ()

Function: Method to return the Unicode encoding of the character at the specified position

var str= "This is a string of strings";
Console.log (str.charcodeat (0))
//This =>36825

3.string.fromcharcode ()

Function: Returns the corresponding character by Unicode encoding

Console.log (String.fromCharCode (36825,26159))//This is

Example: Finding a string is not a number

<body>
<input type= "text"/>
<input type= "button" value= "Detection"/>
<script>
var ainp = document.getelementsbytagname (' input ');

Ainp[1].onclick = function () {
  
  var val = ainp[0].value;
  
  if (Detectnum (val)) {
    alert (' Congratulations, ' + val + ' all numbers ');
  } else {
    alert (' input error ');}
;
function Detectnum (str) {
  var n = 0;
  for (var i=0; i<str.length; i++) {
    n = str.charcodeat (i);
    if (n<48 | | n>57) return false;
  }
  return true;
}
</script>
</body>

4. Stringobject.indexof ()

Function: Method returns the position of a specified string value for the first time in a string.
Parameters: Str.indexof (lookup value, start lookup subscript), if the string value to retrieve does not appear, the method returns-1.

Example: Returns the subscript to find the corresponding character

  <script>
  var str = ' Xsxsxscdecdcdxsxsxs ';
  var num = 0;
  var s = ' xs ';
  var arr = [];
  for (; Str.indexof (S, num)!=-1;) {
    num = str.indexof (s, num) + s.length
    arr.push (num)
  }
  console.log (arr)
  </script>

5. Stringobject.lastindexof ()

Function: To find the first occurrence of a specified string value in a string

6. Stringobject.substring ()

Action: Method is used to extract characters from the string mediation between the two specified subscripts.

7. Stringobject.touppercase ()

Function: Letters to uppercase

8. Stringobject.tolowercase ()

Function: Convert Letters to lowercase

9.stringobject.split ()

Function: Method for dividing a string into an array of strings
Parameters: (interception by what character, preserving array to ordinal)

Three ways to use

var str= "121314";

Str.split ("")//[1,2,1,3,1,4];

Str.split ("1")//[, 2,3,4];

Str.split ("", 2)//[1,2]
10.arrobject.join ()

Function: Method is used to put all elements in an array into a string. element is delimited by the specified delimiter.

Two ways to use

var arr = [1,2,3];
Arr.join ("")//123
arr.join ("-")//1-2-3

Example: highlighting the search keyword

  <input type= "text" id= "oin"/>
  <button> button </button>
  var oin = document.getElementById ("Oin" );
  var obtn = document.getelementsbytagname (' button ') [0];
  var str = "The length of the arguments object is determined by the number of arguments, not the number of parameters."
  A formal parameter is a variable that arguments the memory space inside a function, but it
  does not overlap with the memory space of the object. For both arguments and values, the values are synchronized, but for one of
  them that has no value, the value of the condition is not synchronized. The
  following code can be validated. ";
  var h = "";
  Obtn.onclick = function () {
    if (Oin.value = = "") {
      alert ("Input is Empty");
      return False
    }
    var s = oin.value;
    if (Str.indexof (s) = = 1) {
      alert ("No such number");
      return False
    }
    var m = ' <span style= ' background-color:red ' > ' + S + ' </span> ';
    str = Str.split (s);
    h = Str.join (m)
    document.body.innerhtml=h
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.