简体   繁体   English

如何用特定的indexOf将字符替换为字符串中的大写字母?

[英]How to replace a character with an specific indexOf to uppercase in a string?

Im trying to replace a character at a specific indexOf to uppercase. 我正在尝试将特定indexOf处的字符替换为大写。 My string is a surname plus the first letter in the last name, looking like this: "lovisa t" . 我的字符串是一个姓氏加上姓氏中的第一个字母,如下所示: "lovisa t"

I check the position with this and it gives me the right place in the string. 我用这个检查位置,它在字符串中给了我正确的位置。 So the second gives me 8(in this case). 所以第二个给我8(在这种情况下)。

first = texten.indexOf(" ");
second = texten.indexOf(" ", first + 1);

And with this I replace the first letter to uppercase. 并以此替换第一个字母为大写。

var name = texten.substring(0, second);
name=name.replace(/^./, name[0].toUpperCase());

But how do I replace the character at "second" to uppercase? 但是,如何替换大写的“第二”字符?

I tested with 我测试过

name=name.replace(/.$/, name[second].toUpperCase());

But it did´t work, so any input really appreciated, thanks. 但这没有用,所以非常感谢任何输入。

Here's a version that does exactly what your question title asks for: It uppercases a specific index in a string. 这是一个完全符合您的问题标题要求的版本:它大写字符串中的特定索引。

 function upperCaseAt(str, i) { return str.substr(0, i) + str.charAt(i).toUpperCase() + str.substr(i + 1); } var str = 'lovisa t'; var i = str.indexOf(' '); console.log(upperCaseAt(str, i + 1)); 


However, if you want to look for specific patterns in the string, you don't need to deal with indices. 但是,如果要在字符串中查找特定模式,则无需处理索引。

 var str = 'lovisa t'; console.log(str.replace(/.$/, function (m0) { return m0.toUpperCase(); })); 

This version uses a regex to find the last character in a string and a replacement function to uppercase the match. 此版本使用正则表达式查找字符串中的最后一个字符,并使用替换函数将匹配结果大写。


 var str = 'lovisa t'; console.log(str.replace(/ [az]/, function (m0) { return m0.toUpperCase(); })); 

This version is similar but instead of looking for the last character, it looks for a space followed by a lowercase letter. 这个版本是相似的,但是不是寻找最后一个字符,而是寻找一个空格,后跟一个小写字母。


 var str = 'lovisa t'; console.log(str.replace(/(?:^|\\s)\\S/g, function (m0) { return m0.toUpperCase(); })); 

Finally, here we're looking for (and uppercasing) all non-space characters that are preceded by the beginning of the string or a space character; 最后,我们在这里查找(并大写)所有以字符串开头或空格字符开头的非空格字符; ie we're uppercasing the start of each (space-separated) word. 即我们用大写字母表示每个(以空格分隔)单词的开头。

Your error is the second letter isn't in position 8 , but 7 . 您的错误是第二个字母不在第8 ,而是第7 Also this second = texten.indexOf(" ", first + 1); 同样这second = texten.indexOf(" ", first + 1); gives -1 , not 8 , because you do not have a two spaces in your string. 给出-1 ,而不是8 ,因为字符串中没有两个空格。

If you know that the string is always in the format surname space oneLetter and you want to capitalize the first letter and the last letter you can simply do this: 如果您知道该字符串始终采用姓氏格式“ oneLetter”,并且希望将首字母和最后一个字母大写,则只需执行以下操作:

 var name = 'something s'; name = name[0].toUpperCase() + name.substring(1, name.length - 1) + name[name.length -1].toUpperCase(); console.log(name) 

所有这些都可以通过正则表达式替换完成。

"lovisa t".replace(/(^|\\s)\\w/g, s=>s.toUpperCase());

Try this one (if it will be helpfull, better move constants to other place, due performance issues(yes, regexp creation is not fast)): 试试这个(如果有帮助,最好将常量移到其他地方,由于性能问题(是的,创建正则表达式不是很快)):

function normalize(str){
            var LOW_DASH = /\_/g;
            var NORMAL_TEXT_REGEXP = /([a-z])([A-Z])/g;
            if(!str)str = '';
            if(str.indexOf('_') > -1) {
                str = str.replace(LOW_DASH, ' ');
            }
            if(str.match(NORMAL_TEXT_REGEXP)) {
                str = str.replace(NORMAL_TEXT_REGEXP, '$1 $2');
            }
            if(str.indexOf(' ') > -1) {
                var p = str.split(' ');
                var out = '';
                for (var i = 0; i < p.length; i++) {
                    if (!p[i])continue;
                    out += p[i].charAt(0).toUpperCase() + p[i].substring(1) + (i !== p.length - 1 ? ' ' : '');
                }
                return out;
            } else {
                return str.charAt(0).toUpperCase() + str.substring(1);
            }
        }

console.log(normalize('firstLast'));//First Last
console.log(normalize('first last'));//First Last
console.log(normalize('first_last'));//First Last

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

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