【问题标题】:C++ how can I check if a string contains a special character, then a specific character after?C ++如何检查字符串是否包含特殊字符,然后是特定字符?
【发布时间】:2021-11-21 11:06:19
【问题描述】:

我是一名初级程序员,试图让用户在文本字段中输入电子邮件。在文本字段中,我想:

A) 确保用户在字段中的某处输入了 @ 符号
B) 确保用户输入了 .在@符号之后的某个地方

有没有办法做到这一点?我很难在网上寻找自己的答案,因为它看起来很具体。

感谢您的帮助!

【问题讨论】:

  • 也许显示一些您尝试过的代码。如果您在文本字段中使用 std::string 类型,那么有相当简单的方法可以检查此类内容。
  • 你知道如何迭代字符串中的值吗?这是做其他事情之前的第一步。然后就是寻找角色,并找出与其他角色相比的位置。带着你尝试过的东西回来,也许还有一个更精确的问题。
  • @ShadowMitia 我没有。我没有编写任何代码,因为我不确定从哪里开始。我希望我可以指出一些资源材料的方向来阅读或观看以了解如何。
  • 您正在寻找std::string::find。 (网络搜索“C++ 查找字符串中的字符”会出现很多建议。我怀疑您的搜索过于具体。)
  • 电子邮件格式比这更复杂。通常,验证电子邮件地址的最佳方法就是发送邮件(无论如何您可能都必须发送它)。 (当您的有效电子邮件地址因为不是“传统”而被拒绝时,这很无聊)。

标签: c++ string


【解决方案1】:

你可以使用std::string:

std::string::size_type position = text_field.find(special_char);
if (position != std::string::npos)
{
   // special char is found.
   if (position + 1 < text_field.length())
   {
       if (text_field[position + 1] == specific_char)
       {
            // Specific char found.
       }
   }
}

在使用上述代码之前,您可能需要将文本字段数据类型复制到std::string

【讨论】:

  • 问题的标题模棱两可,但文中说特定字符应该是特殊字符“某处之后”。此代码检查它是否紧随其后。
  • 在 OP 的编辑之前,问题说明要查看下一个字符。
【解决方案2】:

要检查特定字符,请使用

std::string text = "whatever text @ something.com";

auto pos = std::find(text.begin(), text.end(), '@');

如果'@' 出现在text 的某处,pos 将指向第一次出现的位置。

完成后,要搜索第一个字符之后的另一个字符,只需使用相同的方法,从第一个字符的位置开始:

auto res = std::find(pos, text.end(), '.');

如果两次搜索都成功,res 将指向第一个 '.' 之后的第一个 '@'。如果任一搜索失败,它将指向字符串的末尾:

if (res == text.end()) 
    std::cout << "match failed\n";
else
    std::cout << "success\n";

【讨论】:

    【解决方案3】:
        #include <iostream>
        #include <string>
    
    
        int main()
        {
        std::string email;
        std::cin >> email;
        
        bool at_the_rate = false;
        bool dot = false;
        
        for (int i = 0; i < email.size(); ++i) {
                if (email[i] == '@')
                        at_the_rate = true;
                for (int j = i+1; j < email.size(); ++j)
                        if(email[j] == '.')
                           dot = true;
        }
        
        if ( at_the_rate && dot)
                std::cout << "this is valid email." << std::endl;
        else
                std::cout << "this is not a valid email." << std::endl;
        
        return 0;
        
     }
    

    【讨论】:

      【解决方案4】:

      Meme

      如果我没记错的话,那么检查E-Mail有效性的真正正则需要100500行,你决定你真的需要检查电子邮件是否有效,或者它只是满足你的深层主观要求? 但我仍然尝试将其设为正则表达式。

      #include <iostream>
      #include <regex>
      #include <string>
       
      bool CheckEmail(const std::string &str)
      {
          return std::regex_match(str, std::regex("([\\w-\\.]+)@((?:\\w+\\.)+)([a-zA-Z]{2,4})"));
      }
       
      int main()
      {
          std::cout << std::boolalpha << CheckEmail("abra@a.ruuqq") << std::endl;
      }
      

      会有用的...

      https://docs.microsoft.com/en-us/cpp/standard-library/regex-functions?view=msvc-160

      https://en.cppreference.com/w/cpp/regex

      https://www.youtube.com/watch?v=9K4N6MO_R1Y

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      • 如所写,R"(.*@.*\..*)" 符合 OP 要求(但正如您所说,不是电子邮件验证)。
      猜你喜欢
      • 2010-12-20
      • 2016-08-24
      • 2016-08-01
      • 2014-01-08
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      相关资源
      最近更新 更多