0

Let's say for example I have a string

thisIsThisTuesday Day

I want to find the index of all the capital letters, test if there is a space before it, and if not insert one. I would need the index of each one. At least from what I can see indexOf(String) will only produce the index of the first occurance of the character T/t

This :

for(i=0;i<str.length;i++){
  let char=str[i];

  if(isNaN(char*1)&&char==char.toUpperCase()){
    y=str.indexOf(char);
    console.log(char,y)
  }
}

would produce the capital letters, and their indexes but will only display the first occurrence of the character in question. I feel pretty confident that the part I am missing is a for() loop in order to move the index iteration..but it escapes me.

Thank you in advance!

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
BrBearOFS
  • 3
  • 2
  • Maybe not a direct dupe but you can probably use the approach that answers the following question. https://stackoverflow.com/questions/6055152/finding-uppercase-characters-within-a-string – SamuelB Dec 10 '18 at 11:15
  • Possible duplicate of [Finding uppercase characters within a string](https://stackoverflow.com/questions/6055152/finding-uppercase-characters-within-a-string) – TessavWalstijn Dec 10 '18 at 11:15

4 Answers4

2

You can use a regex:

It matches any non-whitespace character followed by a capital letter and replaces it by the two characters with a space between.

const str = "thisIsThisTuesday Day";
const newstr = str.replace(/([^ ])([A-Z])/g, "$1 $2");
console.log(newstr);
georg
  • 211,518
  • 52
  • 313
  • 390
Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38
1

You can use the following regular expression:

/(?<=\S)(?=[A-Z])/g

The replace will insert spaced between characters which are non-space followed by a capital letter.

See example below:

let str = "thisIsThisTuesday Day";
const res = str.replace(/(?<=\S)(?=[A-Z])/g, ' ');

console.log(res);

Note: As pointed out ?<= (positive lookbehind) is currently not be available in all browsers.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 1
    Yup , sorry about not looking into the regex. ywc – Arunprasanth K V Dec 10 '18 at 11:30
  • NB: lookbehind assertions are ES2018, and not universally supported yet. – georg Dec 10 '18 at 11:36
  • @georg ah, yes. Thanks for pointing this out. I've edited my answer – Nick Parsons Dec 10 '18 at 11:39
  • Regex has always been an achilles heal for me. But ultimately is much more efficient I believe.. and I think now that you have been so kind as to point that out so blatently for me.( in a good way) I really need to hone that skill a bit further.. – BrBearOFS Dec 11 '18 at 10:50
  • Thank you all for your time and effort in assisting with this ! – BrBearOFS Dec 11 '18 at 10:50
  • @BrBearOFS no worries, I don’t claim to be an expert in regex either, I just know a few basics because it is an incredibly powerful tool, and having the basics can allow you to do complex things like this rather easily. All it takes is a little bit of practice :) – Nick Parsons Dec 11 '18 at 10:53
0

You could iterate over the string and check if each character is a capital. Something like this:

const s = 'thisIsThisTuesday Day';

const format = (s) => {
    
    let string = '';

    for (let c of s) {

        if (c.match(/[A-Z]/)) string += ' ';

        string += c;
    }

    return string;
};

console.log(format(s));

Or alternatively with reduce function:

const s = 'thisIsThisTuesday Day';

const format = (s) => s.split('').reduce((acc, c) => c.match(/[A-Z]/) ? acc + ` ${c}` : acc + c, '');

console.log(format(s));
Alex G
  • 1,897
  • 2
  • 10
  • 15
0

Actually, the String.indexOf function can take a second argument, specifying the character it should start searching from. Take a look at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

But, if you just want to find all capital letters and prefix them with a space character, if one is not found, there are many approaches, for example:

var str = "thisIsThisTuesday Day";
var ret = '';
for (var i=0; i<str.length; i++) {
    if (str.substr(i, 1) == str.substr(i, 1).toUpperCase()) {
        if ((i > 0) && (str.substr(i - 1,1) != " "))
            ret += " ";
    }
    ret += str.substr(i,1);
}

After running this, ret will hold the value "this Is This Tuesday Day"

georg
  • 211,518
  • 52
  • 313
  • 390
Spyros P.
  • 296
  • 3
  • 14