JavaScript strings (String) object

Source: Internet
Author: User

JavascriptString Object

A String object is used to handle an existing block of characters.

JavaScript string

A string is used to store a series of characters like "John Doe".

A string can use either single or double quotation marks:

Example var carname= "Volvo XC60";
var carname= ' Volvo XC60 ';

You use the location (index) to access any character in the string:

Example Var character=carname[7];

The index of the string starts at zero, so the first character of the string is [0], the second character is [1], and so on.

You can use quotation marks in a string, as in the following example:

Example var answer= "It ' s Alright";
var answer= "He is called ' Johnny '";
var answer= ' He is called ' Johnny ';

Or you can use the escape character in the string to quote:

Example var answer= ' It ' s alright ';
var answer= "He is called" Johnny "";
Try it»

Strings (String)

String (string) to calculate the length of the string using length property:

Example var txt= "Hello world!";
document.write (txt.length);

var txt= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write (txt.length);
Try it» Find a string in a string

The string uses indexOf () to position the first occurrence of a specified character in a string:

Example var str= "Hello World, Welcome to the universe";
var n=str.indexof ("Welcome");
Try it»

If the corresponding character function is not found, return-1

The LastIndexOf () method starts at the end of the string to find where the string appears.

Content Matching

The match () function is used to find a specific character in a string and, if found, returns the character.

Example var str= "Hello world!";
document.write (Str.match ("World") + "<br>");
document.write (Str.match ("World") + "<br>");
document.write (Str.match ("world!"));
Try it»

Replace content

The Replace () method replaces some character with some characters in a string.

Example str= "Please visit microsoft!"
var n=str.replace ("Microsoft", "W3cschool");
Try it»

String Case Conversion

The string case conversion uses the function touppercase () / toLowerCase ():

Example var txt= "Hello world!"; String
var txt1=txt.touppercase (); TXT1 is txt converted to upper
var txt2=txt.tolowercase (); TXT2 is txt converted to lower
Try it»

String to Array

The string is converted to an array using the Strong>split () function:

Instance txt= "A,b,c,d,e"//String
Txt.split (","); Split on commas
Txt.split (""); Split on Spaces
Txt.split ("|"); Split on Pipe
Try it»

Special characters

In Javascript, you can use backslashes (\) to insert special symbols, such as apostrophes, quotation marks, and other special symbols.

Look at the following JavaScript code:

var txt= "We is the so-called" Vikings "from the north.";
document.write (TXT);

In JavaScript, the start and stop of a string use single or double quotation marks. This means that the above string will be cut into: We are the so-called

To resolve the above problems, you can use backslashes to escape quotation marks:

var txt= "We is the so-called \" Vikings\ "from the north.";
document.write (TXT);

JavaScript will output the correct text string: We are the "Vikings" from the so-called.

The following table lists other special characters that you can use to escape special characters by using backslashes:

Code Output
\‘ Single quotation marks
\" Double quotes
\\ Diagonal Bar
\ n Line break
\ r Enter
\ t tab
\b Space
\f Page change

String properties and Methods

Property:

    • Length
    • Prototype
    • Constructor

Method:

      • CharAt ()
      • charCodeAt ()
      • Concat ()
      • fromCharCode ()
      • IndexOf ()
      • LastIndexOf ()
      • Match ()
      • Replace ()
      • Search ()
      • Slice ()
      • Split ()
      • SUBSTR ()
      • SUBSTRING ()
      • toLowerCase ()
      • toUpperCase ()
      • ValueOf ()

JavaScript strings (String) object

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.