javascript
用JavaScript将字符串中的单词大写
String in JavaScript is a sequence of characters. And capitalizing characters of words in a JavaScript string will change each character of the string with the capital letter of it.
JavaScript中的字符串是字符序列。 而將JavaScript字符串中的單詞大寫會更改字符串中每個大寫字母的字符。
To convert words to uppercase we have the following method defined by slicing each character and then converting it to uppercase.
要將單詞轉換為大寫,我們可以通過對每個字符進行切片然后將其轉換為大寫來定義以下方法。
Components are defined as:
組件定義為:
Slice():
The slice method will return the specified element of the object and returns it to another object.
片():
slice方法將返回對象的指定元素,并將其返回到另一個對象。
For slice(3) for the array [3,5,6,8,9] will give 8.
對于數組[3,5,6,8,9]的slice(3)將得到8。
toUpperCase():
The method will return the uppercase changed the value of the character. For example: for 'f' input, the output is 'F'.
toUpperCase():
該方法將返回大寫更改的字符值。 例如:對于“ f”輸入,輸出為“ F”。
charAt():
returns the charter at the given index.
charAt():
返回給定索引的憲章。
Program to capitalize each character of the sting using JavaScript
程序使用JavaScript大寫字符串的每個字符
< script >function capitalizeEachWord(str) {var cstr = '';str.split('').forEach(function(char) {cstr = cstr + char.toUpperCase();});return cstr;}console.log("Hello, world!")var x = "learning JavaScript at include help"var y = capitalizeEachWord(x);console.log('x = ' + x);console.log('y = ' + y); </script>Output
輸出量
Hello, world! learning JavaScript at include help LEARNING JAVASCRIPT AT INCLUDE HELP翻譯自: https://www.includehelp.com/code-snippets/capitalize-words-in-a-string-in-javascript.aspx
總結
以上是生活随笔為你收集整理的用JavaScript将字符串中的单词大写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 使用注解实现消息广
- 下一篇: arm中clz指令_JavaScript