简体   繁体   English

substr()无法按预期方式连接字符串javascript

[英]substr() not behaving as expected to concatenate string javascript

I'm trying to create a function that returns a string in camel case capitalizing each letter where there is a hyphen. 我正在尝试创建一个在驼峰式大小写中返回字符串的函数,该字符串在有连字符的情况下将每个字母大写。 ie "this-should-be-camel-case" would return "thisShouldBeCamelCase" . "this-should-be-camel-case" "thisShouldBeCamelCase" "this-should-be-camel-case"将返回"thisShouldBeCamelCase"

I've changed to first letter of each word to capital and removed to hyphen but can't get the completed sentence to return. 我已将每个单词的首字母更改为大写,并将其删除为连字符,但无法获得完整的句子以返回。 I've tried playing with substr and substring to no avail, not sure if they're what I want to be using for the task. 我尝试使用substrsubstring毫无用处,不确定它们是否是我要用于任务的内容。

What should I be using to return the sentence in camel case ? 在骆驼案中,我应该用什么来退还这句话? How do I do this ? 我该怎么做呢 ? Thanks 谢谢

function camelCaseIt(str) {

for(var i = 0; i < str.length; i++) {

    if(str[i] == '-') {
        var chgLtr = str[i] = '';
        var uprCase = str[i+1].toUpperCase();

    }
}
return str;
}

alert(camelCaseIt('this-should-be-camel-cased'));

This can be accomplished fairly simply with a regular expression: 可以使用正则表达式相当简单地完成此操作:

function camelCaseIt(str) {
    return str.replace(/-([a-z])?/g, function(v) { return (v.length > 1 ? v[1].toUpperCase() : ''); });
}

In your function, you're returning the original (unmodified string). 在您的函数中,您将返回原始(未修改的字符串)。 Instead, you should be building a separate (new) string and returning that: 相反,您应该构建一个单独的(新)字符串并返回:

function camelCaseIt(str) {
    var modifiedString = '';
    for(var i = 0; i < str.length; i++) {
        if(str.charAt(i) == '-') {
            // hyphen; if there is another character, upper-case it and append
            if ((i + 1) < str.length) {
                modifiedString = str.charAt(i + 1).toUpperCase();
            }
        } else {
            // normal character; append it as-is
            modifiedString += str.charAt(i);
        }
    }
    return modifiedString;
}

However, you should be able to accomplish the same thing with a simple regex: 但是,您应该可以使用简单的正则表达式来完成相同的任务:

str = str.replace(/-([a-z])/g, function (match) {
    return match[1].toUpperCase();
});

Indexing the characters of a string as though it's a character array may not work in all browsers (it's nonstandard behavior prior to EMCAScript 5). 将字符串的字符当作一个字符数组进行索引可能无法在所有浏览器中使用(在EMCAScript 5之前是非标准行为)。 While it's correct for browsers compatible with the newest JS standard, you shouldn't use that syntax if you want to support older browsers. 虽然它对于与最新JS标准兼容的浏览器是正确的,但如果要支持较旧的浏览器,则不应使用该语法。 (And even in EMCAScript 5, you can't do for example str[i] = '' .) (甚至在EMCAScript 5中,您也无法执行例如str[i] = '' 。)

function camelCaseIt(str) {
    // create an array ['this', 'should', 'be', 'camel', 'cased']
    var parts = str.split('-');

    // start at i = 1 to skip the first word
    for (var i = 1; i < parts.length; i++) {
        // create an array ['s', 'h', 'o', 'u', 'l', 'd']
        var ltrs = parts[i].split('');
        // ltrs is now ['S', 'h', 'o', 'u', 'l', 'd']
        ltrs[0] = ltrs[0].toUpperCase();
        // parts is now ['this', 'Should', 'Be', 'Camel', 'Cased']
        parts[i] = ltrs.join('');
    }

    // return thisShouldBeCamelCased
    return parts.join('');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM