Javascript 查找字符串中的大写字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6055152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:03:27  来源:igfitidea点击:

Finding uppercase characters within a string

javascriptstring

提问by user753420

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). The function needs to search through the encrypted message for all the uppercase letters and then returns the uppercase character along with lower case that follows it. I have been given a function to call on within the decrypt function:

我正在尝试编写一个函数来解密具有大写字母(显示其新单词)和小写字符(即单词本身)的加密消息。该函数需要在加密的消息中搜索所有大写字母,然后返回大写字符和后面的小写字母。我已经获得了一个在解密函数中调用的函数:

function isUpperCase(aCharacter)    
{    
    return (aCharacter >= 'A') && (aCharacter <= 'Z');
}

I was thinking that I would search through the word for all the uppercase characters first and assign that as a new string. I could then do while loop that will pick up each of the letters in the new string and then search for the lower case characters that are next to it in the old string.

我想我会先在单词中搜索所有大写字符,然后将其分配为一个新字符串。然后我可以做 while 循环,它会选择新字符串中的每个字母,然后在旧字符串中搜索它旁边的小写字符。

However, I am completely stuck at the first part - I cant even work out the structured English.

然而,我完全被第一部分困住了——我什至无法弄清楚结构化的英语。

The code is:

代码是:

  • encryptMessageis a string containing uppercase and lowercase characters
  • indexCharacteris used at a later date for another function
  • upperAlphabet- alphabet of uppercase characters - used later
  • lowerAlphabet- alphabet lowercase characters - used later
  • encryptMessage是一个包含大写和小写字符的字符串
  • indexCharacter稍后用于另一个功能
  • upperAlphabet- 大写字母表 - 稍后使用
  • lowerAlphabet- 字母小写字符 - 稍后使用

The function:

功能:

function decryptMessage(encryptMessage, indexCharacter, upperAlphabet, lowerAlphabet)
{
    var letter
    var word = "";

    for (var count = 0; count < encryptMessage.length; count = count +1);
    {
        letter = encryptMessage.charAt(count) 
        if (isUpperCase(letter));
        { 
            word = word + letter;       
        }
        document.write(word); //this is just to test to see if it returns the uppercase - I would use the return word
    }

The above just doesnt seem to work, so I cant even continue with the rest of the code. Can anyone help me identify where i have gone wrong - have I completely gone the wrong direction with this anyway, reading it back I dont think it really makes much sense ?? Its a very basic code, I have only learnt, for, while loops - if and else functions really, i am just soooooo stuck.

以上似乎不起作用,所以我什至无法继续执行其余的代码。任何人都可以帮助我确定我哪里出错了 - 无论如何,我是否完全走错了方向,再读一遍我认为这真的没有多大意义?它是一个非常基本的代码,我只学习了 for,while 循环 - if 和 else 函数真的,我只是被困住了。

thanks in advance for your advice :-)

提前感谢您的建议:-)

Issy

伊西

采纳答案by Christian

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");
  • 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.

您可以使用正则表达式用更少的代码来做同样的事情,但是由于您说您正在学习非常基础的编程课程,因此该解决方案可能更合适。

回答by Matty F

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'.

在这种情况下,newmsg = 'MESSAGE'。

回答by Brian Peacock

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
*/