如何在c ++字符串中使用+作为字符

时间:2018-02-10 22:36:09

标签: c++ string

我正在创建一个bool函数来测试输入字符串的有效性。其中一个规范要求测试+符号的位置。但是,当我尝试搜索' +'在字符串中,没有任何结果。我在想这是因为+是一个运营商?我也尝试过使用' +'并在此位置创建子字符串但没有成功。

简化版代码:

bool isValidString(string s)
{
    size_t found1 = s.find_first_not of("123456789") //string should only contain numbers, B, and +
    if ( s[found1] == 'B' ) {
        found1++;
        if s[found1] == '+'
            return true;
        else
            return false; }
}

2 个答案:

答案 0 :(得分:1)

如果我猜你的意图......

以下是3种检测字符串中B +'的方法,但第3种方法不符合您的要求。

#include <iostream>
#include <iomanip>
#include <vector>

// returns true when "B+" found in s
bool isValidString1 (std::string s)
{
   bool retVal = false;
   size_t found1 = s.find_first_not_of("123456789"); //string should only contain numbers, B, and +
   if ( s[found1] == 'B' )
   {
      found1++;
      retVal = (s[found1] == '+');
   }
   return retVal;
}

bool isValidString2  (std::string s)
{
   size_t found1 = s.find_first_not_of("123456789"); //string should only contain numbers, B, and +
   bool retVal = false;
   switch (s[found1])
   {
   case 'B': retVal = ('+' == s[found1+1]);      break;
   case '+': /* tbd - what do if out of order */ break;
   default : /* tbd - what do if not allowed  */ break;
   }
   return (retVal);
}

// simple, but does not reject the rest of non-digits 
bool isValidString3 (std::string s)
{
   size_t indx = s.find("B+");
   return (indx != std::string::npos);
}

void test(std::string s)
{
   std::cout << "\n  1  s: '" << s << "'  "
             << (isValidString1(s) ? "valid" : "invalid");

   std::cout << "\n  2  s: '" << s << "'  "
             << (isValidString2(s) ? "valid" : "invalid");

   std::cout << "\n  3  s: '" << s << "'  "
             << (isValidString3(s) ? "valid" : "invalid") << std::endl;
}

int main(int , char** )
{
   std::string s10 = "1234B+56789";
   test(s10);

   std::string s11 = "1234+B+5678";
   test(s11);

   std::string s12 = "B+12345678";
   test(s12);

   std::string s13 = "12345678B+";
   test(s13);

   std::string s14 = "12345678+B";
   test(s14);
}

输出如下:

  1  s: '1234B+56789'  valid
  2  s: '1234B+56789'  valid
  3  s: '1234B+56789'  valid

  1  s: '1234+B+5678'  invalid
  2  s: '1234+B+5678'  invalid
  3  s: '1234+B+5678'  valid

  1  s: 'B+12345678'  valid
  2  s: 'B+12345678'  valid
  3  s: 'B+12345678'  valid

  1  s: '12345678B+'  valid
  2  s: '12345678B+'  valid
  3  s: '12345678B+'  valid

  1  s: '12345678+B'  invalid
  2  s: '12345678+B'  invalid
  3  s: '12345678+B'  invalid

看来以下(isValidStr3()的输出)实际上并不是你想要的。

  3  s: '1234+B+5678'  valid

我发现&#39; +&#39;没有问题,它只是std :: string中的另一个字符。不需要逃脱。在这种情况下,它不是运营商。

答案 1 :(得分:0)

这似乎有用

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str{ "Smith, where Jones + + \"+ +\", \"+ +\" +."
        " \"+ +\" + + the examiners' approval." };
    string substr{ '+' };
    cout << "The string to be searched is:" << endl << str << endl;
    size_t offset{};
    size_t count{};
    size_t increment{ substr.length() };
    while (true)
    {
        offset = str.find(substr, offset);
        if (string::npos == offset)
            break;
        offset += increment;
        ++count;
    }
    cout << " The string \"" << substr
        << "\" was found " << count << " times in the string above."
        << endl;
    return 0;
}