Home  >  Article  >  System Tutorial  >  String processing exercises~

String processing exercises~

PHPz
PHPzforward
2024-03-15 09:13:14831browse

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>

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

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete