字符串

字符串首字母大写

//这个我也一直在纠结,英文标题,即使是首字母大写,也未必每一个单词的首字母都是大写的,但是又不知道哪些应该大写,哪些不应该大写
//ecDo.titleCaseUp('this is a title')
//"This Is A Title"
titleCaseUp: function (str, splitType) {
    var _splitType = splitType || /s+/g;
    var strArr = str.split(_splitType),
        result = "", _this = this
    strArr.forEach(function (item) {
        result += _this.changeCase(item, 1) + ' ';
    })
    return this.trim(result, 4)
}

字符串找出最长单词

//ecDo.longestWord('Find the Longest word in a String')
//result:7
//ecDo.longestWord('Find|the|Longest|word|in|a|String','|')
//result:7
longestWord: function (str, splitType) {
    var _splitType = splitType || /s+/g,
        _max = 0,_item='';
    var strArr = str.split(_splitType);
    strArr.forEach(function (item) {
        if (_max < item.length) {
            _max = item.length
            _item=item;
        }
    })
    return {el:_item,max:_max};
}
原文地址:https://www.cnblogs.com/ly1368489670/p/13915372.html