如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
当前回答
最简单的解决方案是:
let yourSentence = 'it needs first letter upper case';
yourSentence.charAt(0).toUpperCase() + yourSentence.substr(1);
或:
yourSentence.charAt(0).toUpperCase() + yourSentence.slice(1);
或:
yourSentence.substr(0, 1).toUpperCase() + yourSentence.substr(1);
其他回答
试试这个代码:
alert("hello".substr(0, 1).toUpperCase() + "hello".substr(1));
它正在采取“你好”中的第一个字符,资本化它,并添加其余的。
创建一行资本的第一字母
第一個解決方案
“这是一个测试” → “这是一个测试”
var word = "this is a test"
word[0].toUpperCase();
他说:“这是一个测试。
第二個解決方案 第一個字的條件資本
“这是一个测试” → “这是一个测试”
function capitalize(str) {
const word = [];
for(let char of str.split(' ')){
word.push(char[0].toUpperCase() + char.slice(1))
}
return word.join(' ');
}
capitalize("this is a test");
他说:“这是一个测试。
資本化和無資本化第一條線。
功能包括:
/** First Character uppercase */
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
/** First Character lowercase */
function uncapitalize(str) {
return str.charAt(0).toLowerCase() + str.slice(1);
}
例1“第一个字符上方”:
alert(capitalize("hello world"));
标签:Hello World
示例2“第一字符下载案例”:
alert(uncapitalize("Hello World, today is sunny"));
此分類上一篇: Hello World, Today Is Sunny
const capitalizeName = function (name) {
const names = name.split(' ');
const namesUpper = [];
for (const n of names) {
namesUpper.push(n.replace(n[0], n[0].toUpperCase()));
}
console.log(namesUpper.join(' '));
};
capitalizeName('the Eiffel Tower')
函数 capitalizeEachWord(str) { return str.replace(/\w\S*/g,函数(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); } document.write(capitalizeEachWord('foo BAR God bAD'));