JavaScript String Object With Example

String Properties

  • Length-Returns the length of the string.
  • Prototype-the prototype allow you to add properties and methods to an object.

  • String Methods:

    Method Description
    charAt(position) Returns the character at the specified position (in Number).
    charCodeAt(position) Returns a number indicating the Unicode value of the character at the given position (in Number).
    concat([string,,]) Joins specified string literal values (specify multiple strings separated by comma) and returns a new string.
    indexOf(SearchString, Position) Returns the index of first occurrence of specified String starting from specified number index. Returns -1 if not found.
    lastIndexOf(SearchString, Position) Returns the last occurrence index of specified SearchString, starting from specified position. Returns -1 if not found.
    localeCompare(string,position) Compares two strings in the current locale.
    match(RegExp) Search a string for a match using specified regular expression. Returns a matching array.
    replace(searchValue, replaceValue) Search specified string value and replace with specified replace Value string and return new string. Regular expression can also be used as searchValue.
    search(RegExp) Search for a match based on specified regular expression.
    slice(startNumber, endNumber) Extracts a section of a string based on specified starting and ending index and returns a new string.
    split(separatorString, limitNumber) Splits a String into an array of strings by separating the string into substrings based on specified separator. Regular expression can also be used as separator.
    substr(start, length) Returns the characters in a string from specified starting position through the specified number of characters (length).
    substring(start, end) Returns the characters in a string between start and end indexes.
    toLocaleLowerCase() Converts a string to lower case according to current locale.
    toLocaleUpperCase() Converts a sting to upper case according to current locale.
    toLowerCase() Returns lower case string value.
    toString() Returns the value of String object.
    toUpperCase() Returns upper case string value.
    valueOf() Returns the primitive value of the specified string object.


    String HTML wrappers

    Here is a list of methods which returns the string wrapped inside the appropriate HTML tag

    Method Description
    anchor() Creaters an HTML anchor that is used as a hypertext target.
    big() Creates a string to be displayed in a big font as if it were in <big> tag.
    blink() Creates a string to blink as if it were in a <blink> tag.
    bold() Creates a string to be displayed as bold as if it were in a <b> tag.
    fixed() Causes a string to e displayed in fixed-pitch font as if it were in a <tt> tag.
    fontcolor() Causes a string to be displayed in the specified color as if it were in a <font color="color"> tag.
    fontsize() Causes a string to be displayed in the specified font size a if it were in a <font size="size"> tag.
    italics() Causes a string to be italic, as if it were in an <i> tag.
    link() Creates an HTML hypertext link that requests another URL.
    small() Causes a string to be displayed in a small font, as if it were in a <small>tag.
    strike() Causes a string to e displayed as struck-out text, as if it were in a <strike> tag.
    sub() Causes a string to be displayed as a suscript, as itf it were <sub>tag.
    sup() Causes a string to be displayed as a superscript, as if it were in a <sup>tag.


    Read Also: