如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
当前回答
只是因为这是一个真正的单线,我会包括这个答案. 这是一个基于ES6的交叉线单线。
let setStringName = 'the Eiffel Tower';
setStringName = `${setStringName[0].toUpperCase()}${setStringName.substring(1)}`;
其他回答
使用此 Node.js 模块, http://stringjs.com/ 包,以资本化您的行:
var S = require('string');
S('jon').capitalize().s; //'Jon'
S('JP').capitalize().s; //'Jp'
/*
* As terse as possible, assuming you're using ES version 6+
*/
var upLetter1=s=>s.replace(/./,m=>m.toUpperCase());
console.log(upLetter1("the quick brown fox jumped over the lazy dog."));
//\\ The quick brown fox jumped over the lazy dog. //\\
function cap(input) {
return input.replace(/[\.\r\n\t\:\;\?\!]\W*(\w)/g, function(match, capture) {
// For other sentences in the text
return match.toUpperCase();
}).replace(/^\W*\w/, function(match, capture) {
// For the first sentence in the text
return match.toUpperCase();
});;
}
var a = "hi, dear user. it is a simple test. see you later!\r\nbye";
console.log(cap(a));
// Output: Hi, dear user. It is a simple test. See you later!
// Bye
当我们说资本时,这意味着每个字中的第一个字母应该在上方,而成功的字符则在下方。
第一個函數下有兩個函數,第一個函數將使一條字符的第一個字符在上方,成功的字符在下方,第二個函數將使一條字符在標題字符,這意味著每個字符的第一個字符將在頭部。
// Will make will first letter of a sentence or word uppercase function capital(word){ word = word.toLowerCase() return word[0].toUpperCase() + word.substring(1); } // Will make first letter in each word capital function titleCase(title) { title = title.toLowerCase(); const words = title.split(' '); const titleCaseWords = words.map(word) => word[0].toUpperCase() + word.substring(
只是因为这是一个真正的单线,我会包括这个答案. 这是一个基于ES6的交叉线单线。
let setStringName = 'the Eiffel Tower';
setStringName = `${setStringName[0].toUpperCase()}${setStringName.substring(1)}`;