Finding uppercase characters within a string

前端 未结 3 1052
北海茫月
北海茫月 2020-12-03 21:26

I am trying to write a function that decryptes an encrypted message that has uppercase letters (showing its a new word) and lower case characters (which is the word itself).

相关标签:
3条回答
  • 2020-12-03 21:40

    I'm not too sure I follow, but you can strip using the replace method and regular expressions

    var str = 'MaEfSdsfSsdfsAdfssdGsdfEsdf';
    var newmsg = str.replace(/[a-z]/g, '');
    var old = str.replace(/[A-Z]/g, '');
    

    In this case, newmsg = 'MESSAGE'.

    0 讨论(0)
  • 2020-12-03 21:41

    EDIT

    String input = "ThisIsASecretText";    
    
    for(int i = 0; i < input.Length; i++)
    {
      if(isUpperCase(input.charAt(i))
      {
         String nextWord = String.Empty;
    
         for(int j = i; j < input.Length && !isUpperCase(input.charAt(j)); j++)
         {
           nextWord += input.charAt(j);
           i++;
         }
    
         CallSomeFunctionWithTheNextWord(nextWord);
      }
    }
    

    The following calls would be made:

    • CallSomeFunctionWithTheNextWord("This");
    • CallSomeFunctionWithTheNextWord("Is");
    • CallSomeFunctionWithTheNextWord("A");
    • CallSomeFunctionWithTheNextWord("Secret");
    • CallSomeFunctionWithTheNextWord("Text");

    You can do the same thing with much less code using regular expressions, but since you said that you are taking a very basic course on programming, this solution might be more appropriate.

    0 讨论(0)
  • 2020-12-03 22:01

    A simple condition for checking uppercase characters in a string would be...

    var str = 'aBcDeFgHiJkLmN';
    var sL = str.length;
    var i = 0;
    for (; i < sL; i++) {
        if (str.charAt(i) === str.charAt(i).toUpperCase()) {
            console.log('uppercase:',str.charAt(i));
        }
    }
    
    /*
        uppercase: B
        uppercase: D
        uppercase: F
        uppercase: H
        uppercase: J
        uppercase: L
        uppercase: N
    */
    
    0 讨论(0)
提交回复
热议问题