Uppercase letters of each word separated by spaces in a string

Source: Internet
Author: User

This article describes how to change the first letter of each word in a string to uppercase. For more information, see

In a string, each word is separated by a space, and the number of spaces is unlimited.

The Code is as follows:

Function capitalize (sting ){

Var words = string. split ("");

For (var I = 0; I <words. length; I ++ ){

Words [I] = words [I]. charAt (0). toUpperCase () + words [I]. slice (1 );

}

Return words. join ("");

}

Var string = "ajax cookie event object ";

Capitalize (string); // "Ajax Cookie Event Object"

 

Pay attention to the key sentence in the code

The Code is as follows:

Words [I] = words [I]. charAt (0). toUpperCase () + words [I]. slice (1 );

 

Words [I]. charAt (0 ). toUpperCase () only obtains the first letter of the string and converts it to an uppercase letter. It does not change the original string. Therefore, it must be connected to other characters in the original string and the new value must be assigned to the original string.

 

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.