简体   繁体   中英

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. My string is a surname plus the first letter in the last name, looking like this: "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).

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 . Also this second = texten.indexOf(" ", first + 1); gives -1 , not 8 , because you do not have a two spaces in your string.

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:

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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