如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
当前回答
你可以这样做,这样一行。
string[0].toUpperCase() + string.substring(1)
其他回答
下面是我所使用的功能:
capitalCase(text: string = 'NA') {
return text
.trim()
.toLowerCase()
.replace(/\w\S*/g, (w) => w.replace(/^\w/, (c) => c.toUpperCase()));
}
console.log('this cApitalize TEXt');
使用 Tailwind CSS
<p class="capitalize">The quick brown fox</p>
此分類上一篇: Quick Brown Fox
(src: https://tailwindcss.com/docs/text-transform#transforming-text)
你可以这样做,这样一行。
string[0].toUpperCase() + string.substring(1)
一个小改进 - 每个字在标题。
String.prototype.toTitleCase = function(){
return this.replace(/\b(\w+)/g, function(m,p){ return p[0].toUpperCase() + p.substr(1).toLowerCase() });
}
var s = 'heLLo, wOrLD!';
console.log(s.toTitleCase()); // Hello, World!
尝试下列功能:
function capitalize (string) {
return [].map.call(string, (char, i) => i ? char : char.toUpperCase()).join('')
}
使用:
capitalize('hello, world!')
结果:
Hello, world!