Home > Web Front-end > JS Tutorial > body text

How to capitalize the last letter in JavaScript

青灯夜游
Release: 2022-02-21 18:51:00
Original
1866 people have browsed it

Method: 1. Use the slice() method to divide the string into two parts: "last letter" and "other subcharacters"; 2. Use toUpperCase() to convert the last letter to uppercase, toLowerCase() Convert other subcharacters to lowercase; 3. Use the " " operator to rejoin the two parts to form a new string.

How to capitalize the last letter in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, you can use the slice(), toUpperCase(), toLowerCase() functions and the string splicing character " " to set the first letter to be capitalized.

Implementation idea:

  • Use the slice() method to divide the string into two parts: the last letter character part, and other sub-character parts.

  • Use toUpperCase() method to convert the last letter to uppercase; use toLowerCase() to convert other sub-characters to lowercase.

  • Use the " " operator to rejoin the two parts

Implementation code:

function f(str) {
newStr =  str.slice(0,-1).toLowerCase()+str.slice(-1).toUpperCase();
   console.log(newStr);
}
f("hello World");
f("abcd");
Copy after login

How to capitalize the last letter in JavaScript

Description:

slice(start, end) method can extract a certain part of the string and return the extracted part as a new string.

Use the start (inclusive) and end (exclusive) parameters to specify the part of the string to extract.

Grammar:

string.slice(start,end)
Copy after login

How to capitalize the last letter in JavaScript

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to capitalize the last letter in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!