如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
如果是字母,我该如何创建一条字符的第一个字符,但不会改变其他字符中的任何一个字符的案例?
例如:
“这是一个测试” → “这是一个测试” “埃菲尔塔” → “埃菲尔塔” “/index.html” → “/index.html”
当前回答
var capitalizeMe = "string not starting with capital"
资本化与substr
var capitalized = capitalizeMe.substr(0, 1).toUpperCase() + capitalizeMe.substr(1);
其他回答
带有箭功能
let fLCapital = s => s.replace(/./, c => c.toUpperCase())
fLCapital('this is a test') // "This is a test"
用火箭功能,另一种解决方案
let fLCapital = s => s = s.charAt(0).toUpperCase() + s.slice(1);
fLCapital('this is a test') // "This is a test"
与 Array 和 地图()
let namesCapital = names => names.map(name => name.replace(/./, c => c.toUpperCase()))
namesCapital(['james', 'robert', 'mary']) // ["James", "Robert", "Mary"]
这样做的一个简单的方式是:
如果您想将其添加到 String.prototype:
使用:
var str = “ruby java”; console.log(str.charAt(0).toUpperCase() + str.substring(1));
它将输出“Ruby java”到控制台。
資本化和無資本化第一條線。
功能包括:
/** 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
var a = "this is a test"
console.log(a.replace(/^[a-z]/g, txt => txt.toUpperCase()));