2017-04-06 154 views
0

如果對於單個大寫單詞,我知道如何做,但對於多個大寫單詞不知道該怎麼做。單個單詞:value.toLowerCase()。charAt(0).toUpperCase()+ value.toLowerCase()。slice(1);大寫每個大寫單詞的第一個字母

它會改變「完成」「完成」 但是,如果我有「木已成舟」將其更改爲「木已成舟」,但它應該像「木已成舟」

+1

所以各執字和循環或使用正則表達式的[字符串轉換爲用JavaScript標題情況](HTTP – epascarello

+0

可能重複:// stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) –

回答

0

您應該使用split方法,以在句子中適用每項工作的規則。

var str="done done"; 
 
console.log(str.split(' ').map(function(item){ 
 
    return item.toLowerCase().charAt(0).toUpperCase() + item.toLowerCase().slice(1); 
 
}).join(' '))

+1

謝謝你..完美答案@alexandru –

0

能幫

String line = "DONE DEAL"; 
    String[] words = line.split("\\s"); 
    for(String eachWord: words){ 
     String concat = eachWord.substring(0, 1).toUpperCase().concat(eachWord.substring(1).toLowerCase()); 
     System.out.println(concat); 
    } 
+0

Anlexandru是第一個...感謝您的幫助,雖然 –

相關問題