7kyu Jaden Casing Strings

题目:

Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word.

Jaden Smith是威尔史密斯的儿子,他是电影空手道小子(2010)和地球(2013)的明星。Jaden也因他的一些哲学而闻名,他通过Twitter发布了他的哲学。当他在Twitter上写作时,他几乎总是把每个词都大写。

Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.

你的任务是把字符串转换成Jaden Smith的写作方式。这些字符串是Jaden Smith的实际引用,但它们并没有像他最初输入的那样大写。

Example:

Not Jaden-Cased: "How can mirrors be real if our eyes aren't real"
Jaden-Cased:     "How Can Mirrors Be Real If Our Eyes Aren't Real"

Sample Tests:

var str = "How can mirrors be real if our eyes aren't real";
Test.assertEquals(str.toJadenCase(), "How Can Mirrors Be Real If Our Eyes Aren't Real");

答案:

// 1
String.prototype.toJadenCase = function () {
  var str = this;
  var arr = str.toLowerCase().split(' ');
  for(i = 0; i < arr.length; i++) {
    arr[i] = arr[i].slice(0,1).toUpperCase() + arr[i].slice(1);
  }
  return arr.join(' ');
};

// 2
String.prototype.toJadenCase = function () {
    return this.split(" ").map((word) => {
        return word.charAt().toUpperCase() + word.slice(1);
    }).join(" ");
}

// 3  
// ^    匹配字符串的开始  
// \s匹配任意的空白符,包括空格,制表符(Tab),换行符,中文全角空格等
String.prototype.toJadenCase = function () {
    return this.replace(/(^|\s)[a-z]/g, function(x) {
        return x.toUpperCase();
    });
}

 

转载于:https://www.cnblogs.com/tong24/p/7382649.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值