Home > System Tutorial > LINUX > String processing exercises~

String processing exercises~

PHPz
Release: 2024-03-15 09:13:14
forward
894 people have browsed it

String processing exercises~

The code is as follows: Make sure the first letter of each word in the string is capitalized and the rest is lowercase.

代码如下:
function titleCase(str) {
  //将字符串转化为数组并将其小写化
  var arr = str.toLowerCase().split(" ");
  
  for (var i=0;i<arr.length var char="arr[i].charAt(0);" arr function replace return char.touppercase arr.join str titlecase a little tea pot>
<p><strong>explain:</strong></p>
<p>First convert the string into an array, and then convert it into lowercase. Here str.toLowerCase() and str.split(" ") are used. The split must be separated by spaces, and then use a for loop to traverse the array. For each element in the array, assign the first letter of each element to the variable char, then use the toUpperCase() function to capitalize the first letter, then reassign each element to the array, and finally use join to convert the array into a string. </p></arr.length>
Copy after login

The above is the detailed content of String processing exercises~. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
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