第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將第一個單詞第一個字符轉換為大寫字母?

如何將第一個單詞第一個字符轉換為大寫字母?

動漫人物 2019-04-16 15:15:36
我想要這個:示例:stackoverflow很有幫助。=> Stackoverflow很有幫助。作為示例顯示想要將我的第一個單詞第一個字符轉換為大寫字母。我嘗試下面給出的代碼不起作用,不理解我做錯了請幫忙。<textarea autocomplete="off" cols="30" id="TextInput" name="message" oninput="myFunction()" rows="10" style="width: 100%;"></textarea><input id="FistWordFirstCharcterCapital" onclick="FistWordFirstCharcterCapital()" style="color: black;" type="button" value="First word first character capital!" /> </br></br><script>  function FistWordFirstCharcterCapital() {    var x = document.getElementById("TextInput").value.replace(string[0], string[0].toUpperCase());    document.getElementById("TextInput").value = x;  }</script>
查看完整描述

5 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

將它視為類似數組的對象來獲取第一個字符,將其大寫,然后concat將其視為字符串的其余部分:


const str = "hello World!";

const upper = ([c, ...r]) => c.toUpperCase().concat(...r);

console.log(upper(str));


查看完整回答
反對 回復 2019-05-17
?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

你可以charAt用來獲得第一個字母:


const string = "stackoverflow is helpful."

const capitalizedString = string.charAt(0).toUpperCase() + string.slice(1)


console.log(capitalizedString)


查看完整回答
反對 回復 2019-05-17
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

你不想使用替換,也不想使用string[0]。相反,使用下面的小方法


const s = 'foo bar baz';


function ucFirst(str) {

  return str.substr(0, 1).toUpperCase() + str.substr(1);

}


console.log(ucFirst(s));


查看完整回答
反對 回復 2019-05-17
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

既然你似乎想知道這里有一個詳細的例子:


function FistWordFirstCharcterCapital() {

  let text =  document.getElementById("TextInput").value;

  let firstSpaceIndex = text.indexOf(" ")!=-1 ? text.indexOf(" ")+1:text.length;

  let firstWord = text.substr(0, firstSpaceIndex);

  let firstWordUpper = firstWord.charAt(0).toUpperCase() + firstWord.slice(1)

  document.getElementById("TextInput").value = firstWordUpper + text.substr(firstSpaceIndex);;

}

<textarea autocomplete="off" cols="30" id="TextInput" name="message" rows="10" style="width: 100%;">

</textarea>


<input id="FistWordFirstCharcterCapital" onclick="FistWordFirstCharcterCapital()" style="color: black;" type="button" value="First word first character capital!" />


查看完整回答
反對 回復 2019-05-17
  • 5 回答
  • 0 關注
  • 783 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號