有没有一种方法可以检查字符串中是否有一定数量的字母?

时间:2020-05-05 03:08:37

标签: javascript

我正在制作youtube机器人,唯一的错误是它无法分辨“ ID:54584884”(其GD是必需的)和“ BRO DO MY THING PLSSSSS”之间的区别。有没有一种有效的方法来检查字母是否超过6个字符? (顺便说一句,这是检查所有带帽聊天的方式)。更清楚地说,我要执行的操作就像Nightbot如何在发送所有带帽消息时发出警告一样,我想这样做。我现在正在做的事情是检查字符串和string.toUpperCase()是否相同,但这当然意味着数字可以用作大写字母。

4 个答案:

答案 0 :(得分:0)

您可以看一下正则表达式(有时称为Regex)。它允许您在字符串中搜索特定模式。

答案 1 :(得分:0)

我不明白,但是据我所知,您可以比较2个字符串以检查它们是否相同:

string a,b ;
a= "hello";
b= "hello";
if(a==b)
return 1 ;

答案 2 :(得分:0)

假设消息是字符串,则应该可以正常工作:

message = "This is a message."
up_character_list = []
for character in message:
    if (character.isalpha()):
        if character.isupper():
            up_character_list.append(character)

if (len(up_character_list) > 6):
    # The message has more than 6 upper case alphabets.
    # Do what you want here.

遍历字符串,首先检查字符串中的字符是否为字母,然后检查其是否为大写,如果两者都为真,则将字符添加到列表中。 然后,您可以找到列表的长度,然后从那里进行任何操作。

答案 3 :(得分:0)

字符串是一个字符数组,表示您输入的每个字符串都有一个数字

string a = "hello"

此字符串有5个字符,这意味着如果您告诉编译器将字母打印在第五位,您就可以像数组一样

//pay attention im coding in c++ it must work in java script
cout << a[4]//it means to print the letter in place 5

有一条命令告诉您拥有的数组的大小

a.size();//this is the command that tells you the size of your array which is string in our case
cout << a.size() ;// the answer is 5 

如果您的字符串带有空格,则应使用if语句 为什么? 因为您将拥有数组的大小,但是如果数组中有空格,它将使它的大小变大,这就是为什么您必须键入这样的小代码

#include <iostream>
#include <string>
using namespace std ;
int main ()
{
string The_string = "hello world" ;
  char something_to_store_letters_on;//since i will store space i will use char
     int counter{a.size()};//here i tell the counter that its 11 the size of the the string array
for(i=0;i<a.size(); i++){
 The_string = something_to_store_letters_on.at(i);//in this for loop im storing in char letter by letter to check if its space or not

    if(something_to_store_letters_on==' ')//here i say if its space then the counter have 11(size of string array) to decrease it by 1
    counter-- ;
}
cout << counter ;// here i have the number of the charactes with out spaces
}

这是Q的答案,即如何检查字符串中带空格或带空格的字母,如果您知道数组,我会尽量使其简单,那么您将毫无疑问地理解数组。 希望我能对您有所帮助。