Properties and methods of the Javascript string class

Source: Internet
Author: User

String class Attribute and Method
Anchor () Create an instance of the <a> tag and set its Name property to the string passed to this method
Big () converts a string to an instance of the <big> tag
Blink () Converts a string to an instance of a <blink> tag
Bold () converts a string to an instance of the <bold> tag
CharAt () returns the character at the index passed to this method
The concat () connection returns a new string of two strings that are passed.
Fixed () converts a character to an instance of a pitch font label <tt>
FontColor () Sets the color property of a <font> tag instance
FontSize () Sets the Size property of a <font> tag instance
fromCharCode () returns the iso-latin-1 numeric character that is passed to this party
IndexOf () returns the index of the first occurrence of the string passed to this method in an instance of a Sting object
Italics () converts a string to an instance of a <i> tag
LastIndexOf () returns the index of the last occurrence of the string passed to this method in an instance of an Sting object
Link () Converts the string to an instance of the <a> tag and sets the Herf property with the URL that is passed to this method
Match () returns an array that contains the matches found for the regular expression that is passed to this method.
Replace () performs a lookup and substitution with its strin instance using the rule expression and substitution string that are passed to this method.
Search () returns the matching index position found in the string passed to this method. Returns 1 if the character distance is not found
Slice () returns the string that is passed between the start and end indexes of this method. If a negative value is passed, the index is referenced as the tail of the passed string.
Small () Swap string as an instance of the <small> label
Split () returns a string that is divided into segments, which is defined by the string and instance restrictions passed to this method
Strike () Converts a string to an instance of a <strike> tag
Sub () Converts a string to an instance of the <sub> tag
SUBSTR () returns a string from the indexed position that contains a certain number of characters to return. If a negative value is passed, the index is referenced as the tail of the passed string
SUBSTRING () returns the characters between the start and end indexes that are passed
SUP () Converts a string to an instance of the <sup> tag
toLowerCase () converts all characters in a string to lowercase
Tosource () returns the passed character as a string representation of a String object
ToString () returns the passed character as a string type
toUpperCase () converts all characters in a string to uppercase
Length () returns the lengths of the strings
Prototype () provides a program with the ability to add properties to a String object instance

Extension of the String class:
String.prototype.tochararray=function () {
Return This.split ("");
}
Reverse
String.prototype.Reverse = function () {
Return This.split (""). Reverse (). Join ("");
}
Whether to include the specified character
String.prototype.IsContains = function (str) {
Return (This.indexof (str) >-1);
}
Determines whether the empty
String.prototype.isempty=function () {
return this== "";
}
Determine if it is a number
String.prototype.IsNumeric = function () {
var tmpfloat=parsefloat (this);
if (IsNaN (Tmpfloat)) return false;
var tmplen=this.length-tmpfloat.tostring (). length;
return tmpfloat+ "0". Getsame (Tmplen) ==this;
}
Determine if it is an integer
String.prototype.IsInt = function () {
if (this = = "NaN") return false;
return this = = parseint (this). ToString ();
}
Merge multiple blanks into a blank
String.prototype.resetBlank = function () {
Return This.replace (/s+/g, "");
}
Remove left margin
String.prototype.LTrim = function () {
Return This.replace (/^s+/g, "");
}
Remove right margin
String.prototype.RTrim = function () {
Return This.replace (/s+$/g, "");
}
Remove both sides blank
String.prototype.trim = function () {
Return This.replace (/(^s+) | ( s+$)/g, "");
}
Keep numbers
String.prototype.getNum = function () {
Return This.replace (/[^d]/g, "");
}
Reserved letters
String.prototype.getEn = function () {
Return This.replace (/[^a-za-z]/g, "");
}
Keep Chinese
String.prototype.getCn = function () {
Return This.replace (/[^u4e00-u9fa5uf900-ufa2d]/g, "");
}
Get byte length
String.prototype.bytelength=function ()
{
Return This.replace (/[^\x00-\xff]/g, "AA"). Length;
}
Intercepts a string of a specified length from the left
String.prototype.left = function (n) {
Return This.slice (0,n);
}
To intercept a string of a specified length from the right
String.prototype.right = function (n) {
Return This.slice (THIS.LENGTH-N);
}
HTML encoding
String.prototype.HTMLEncode = function () {
var re = this;
var q1 = [/x26/g,/x3c/g,/x3e/g,/x20/g];
var q2 = ["&", "<", ">", ""];
for (Var i=0;i<q1.length;i++)
Re = Re.replace (Q1[i],q2[i]);
return re;
}
Get Unicode
String.prototype.Unicode = function () {
var tmparr=[];
for (var i=0; i<this.length; i++) Tmparr.push ("a" + this.charcodeat (i) + ";");
Return Tmparr.join ("");
}
Inserts a string at the specified location
String.prototype.insert=function (INDEX,STR) {
Return this.substring (0,index) +str+this.substr (index);
}
Copy string
String.prototype.copy=function () {
if (IE) window.clipboardData.setData ("Text", this.tostring ());
}
Append formatted string
String.prototype.appendformat=function (str) {
var arg=arguments;
Str=str.replace (/\{(\d+) \}/g,function (a,b,c)
{
if (Arg[parseint (b) +1]) return Arg[parseint (b) +1];
else return A;
});
return this+str;
}


Creates a String object, syntax: New string (StringValue), which converts the argument to a string and acts as a string object. In fact, any string constant is a string object that can be used directly as an object, and the difference between creating an object using the new string () is that the return value of typeof is different, one is "stirng" and the other is "object".

String.IndexOf (searchstring,position)----start with the position (optional) position, searching for where the first searchsting in the string appears and returning. For example: "Hello,jack". IndexOf ("Hello") will return 0;
"Abcabc". IndexOf ("A", 1) will return 3;

String.LastIndexOf (searchstring,position)--------start with the position (optional) position, searching for where the last searchstring in the string appears and returning. such as: "Abcabc". LastIndexOf ("B") will return 4

String.charat (POS)-------returns the character in the string where Pos is located. For example: "ABC". CHARAT (1) return "B"

Stirng.charcodeat (POS)------Returns the character code in the string where Pos is located. For ASCII characters, this returns its ASCII code. For example: "ABC". charCodeAt (0) returns 97, which represents the ASCII code of the character "a".

String.slice (Start,end)--------returns a substring in the string starting at start and ending at end (not including end)

String.Split (Separator,linmit)------cut strings into multiple substrings with separator as delimiters and return them as an array. Linmit (optional) represents the maximum length of the array, and the excess portion is discarded. The separator delimiter is not included in any substrings, and if Sepatator is an empty string, an array of character sequences in the string is returned. If the split method does not have any arguments, it returns an array that contains only the string itself and only one element.

String.Split (Separator,linmit)-------For example: "A1,b1,c1". Split (",") will return ["A1", "B1", "C1"];

"A,b,c". Split (",", 2) will return ["A", "B"];

"A,b,c". Split ("") will return ["a", ",", "B", ",", "C"];

"Ab,c". Split () will return ["Ab,c"]
String.substr (start,length)--------returns a substring with a length from start to start in the string. For example: "ABCDEFG". substr (1,3) will return "BCD";

String.substring (start,end)------Returns a substring where the starting position in the string is start and the ending position is end (including end). The only difference between this method and the slice method is the character that contains the end position.


Replacing and matching strings


(1) Replace (Searchvalue,replacevalue) method
The method replaces the first occurrence of the Searchvalue substring in a string with Replacevalue and returns a new string. The original string is not affected.

For example: Var str1= "AAAA";

var str2=str1.replace ("A", "B");

alert (str2);//Output "BAAA"
alert (str1);//Output "AAAA"

As you can see in the code above, you can only replace one instance with the Replace function. If you are replacing multiple instances, you need to use regular expressions, such as str.replace (/a/g, "B") to replace "AAAA" with "bbbb".

(2) match (Reexp) method

Searches the string for all substrings that match the regexp regular expression, returning them as an array. You can also determine whether a string matches the regular expression represented by the regexp by using the conversion rules of the object type to the Boolean type.

For example: Var strinput=prompt ("Please enter a number:", 0);

while (!strinput.match (/\d+/)) {
Strinput=prompt ("Please enter a number:", 0);
}

(3) Search (REGEXP) method

Searches for the first substring of a matching regexp regular expression from a string, returning its index position. For example: Var str= "AABCABCABC";

Alert (Str.search (/abc/g));//display "1"

(4) Case conversion of a String object

var str= "abc";

Str.tolowercase ()//Convert lowercase

Str.touppercase ()//convert capitalization

(5) connection of a String object

var str= "abc";

var str2=str.concact ("Def", "Ghi");

alert (str2);//outputs "Abcdefghi"

Properties and methods of the Javascript string class

Related Article

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.