MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla
검색 결과

    String.prototype

    Summary

    Represents the String prototype object.

    Description

    All String instances inherit from String.prototype. Changes to the String prototype object are propagated to all String instances.

    Properties

    constructor
    오브젝트의 프로토타입 생성에 지정된 함수입니다.
    length
    문자열의 길이가 반영됩니다.
    N
    N번째 위치의 문자 접근에 사용되는 N은 0과 length사이의 정수입니다. 이 프로퍼티는 읽기만 가능합니다.
    Properties inherited from Object:

    Methods

    Methods unrelated to HTML

    These headings are duplicated intentionally so we can maintain document structure in the String article

    Methods unrelated to HTML

    charAt
    Returns the character at the specified index.
    charCodeAt
    Returns a number indicating the Unicode value of the character at the given index.
    concat
    Combines the text of two strings and returns a new string.
    contains

    Determines whether one string may be found within another string.

    endsWith
    Determines whether a string ends with the characters of another string.
    indexOf
    Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
    lastIndexOf
    Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
    localeCompare
    Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
    match
    Used to match a regular expression against a string.
    quote

    Wraps the string in double quotes (""").
    replace
    Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
    search
    Executes the search for a match between a regular expression and a specified string.
    slice
    Extracts a section of a string and returns a new string.
    split
    Splits a String object into an array of strings by separating the string into substrings.
    startsWith
    Determines whether a string begins with the characters of another string.
    substr
    Returns the characters in a string beginning at the specified location through the specified number of characters.
    substring
    Returns the characters in a string between two indexes into the string.
    toLocaleLowerCase
    The characters within a string are converted to lower case while respecting the current locale. For most languages, this will return the same as toLowerCase.
    toLocaleUpperCase
    The characters within a string are converted to upper case while respecting the current locale. For most languages, this will return the same as toUpperCase.
    toLowerCase
    Returns the calling string value converted to lower case.
    toSource

    Returns an object literal representing the specified object; you can use this value to create a new object. Overrides the Object.prototype.toSource method.
    toString
    Returns a string representing the specified object. Overrides the Object.prototype.toString method.
    toUpperCase
    Returns the calling string value converted to uppercase.
    trim
    Requires JavaScript 1.8.1
    Trims whitespace from the beginning and end of the string. Part of the ECMAScript 5 standard.
    trimLeft
    Requires JavaScript 1.8.1
    Trims whitespace from the left side of the string.
    trimRight
    Requires JavaScript 1.8.1
    Trims whitespace from the right side of the string.
    valueOf
    Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf method.

    HTML wrapper methods

    HTML wrapper methods

    Non-standard
    This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

    Should we change these to point to our own docs rather than W3C? In any case, they're staying as is for now because with the current structure of the HTML pages here at MDC, we cannot reference specific attributes as necessary anyway (e.g., the difference between the anchor method and the link method). Each of the following methods returns a copy of the string wrapped inside the appropriate HTML tag.

    anchor
    <a name="name"> (hypertext target)
    big
    <big>
    blink
    <blink>
    bold
    <b>
    fixed
    <tt>
    fontcolor
    <font color="color">
    fontsize
    <font size="size">
    italics
    <i>
    link
    <a href="url"> (link to URL)
    small
    <small>.
    strike
    <strike>
    sub
    <sub>
    sup
    <sup>

    These methods are of limited use, as they provide only a subset of the available HTML tags and attributes.

    Examples

    Example: Extending string instances with a repeat method

    The following example creates a method, str_rep, and uses the statement String.prototype.repeat = str_rep to add the method to all String objects. All String instances then have that method, even objects already created. The example then creates an alternate method and overrides the previous method in one of the String objects using the statement s1.repeat = fake_rep. The str_rep method of the remaining String objects is not altered.

    var s1 = new String("a");
    var s2 = new String("b");
    var s3 = new String("c");
    
    // Create a repeat-string-N-times method for all String objects
    function str_rep(n) {
       var s = "", t = this.toString();
       while (--n >= 0) {
         s += t
       }
       return s;
    }
    
    String.prototype.repeat = str_rep;
    
    s1a=s1.repeat(3); // returns "aaa"
    s2a=s2.repeat(5); // returns "bbbbb"
    s3a=s3.repeat(2); // returns "cc"
    
    // Create an alternate method and assign it to only one String variable
    function fake_rep(n) {
       return "repeat " + this + " " + n + " times.";
    }
    
    s1.repeat = fake_rep
    s1b=s1.repeat(1); // returns "repeat a 1 times."
    s2b=s2.repeat(4); // returns "bbbb"
    s3b=s3.repeat(6); // returns "cccccc"
    

    The function in this example also works on String objects not created with the String constructor. The following code returns "zzz".

    "z".repeat(3);
    

    문서 태그 및 공헌자

    Contributors to this page: teoli, MoGi
    최종 변경: teoli,