您的位置:

C++ string包含指定字符串

一、C++ string包含指定字符串

C++ string类提供了一个成员函数find()来判断一个字符串中是否包含另一个字符串,如果存在,返回被查找字符串第一次出现的位置,否则返回string::npos。使用例子如下:

#include <string>
#include <iostream>

using namespace std;

int main()
{
  string str1 = "Hello, World!";
  string str2 = "World";
  
  if (str1.find(str2) != string::npos)
  {
    cout << "String found in position " << str1.find(str2) << endl;
  }
  else
  {
    cout << "String not found" << endl;
  }
  
  return 0;
}

在本例中,我们使用find()来查找str2字符串是否在str1字符串中出现。如果存在,输出第一次出现的位置;否则输出“String not found”。该程序的输出结果为:

String found in position 7

二、string是否包含字符串

除了find()函数之外,我们也可以使用string::compare()函数来判断一个字符串是否包含另一个字符串,当该函数返回值为0时,表示两个字符串相等,否则表示不相等。使用示例如下:

#include <string>
#include <iostream>

using namespace std;

int main()
{
  string str1 = "Hello, World!";
  string str2 = "World";
  
  if (str1.compare(6, 5, str2) == 0)
  {
    cout << "String found!" << endl;
  }
  else
  {
    cout << "String not found!" << endl;
  }
  
  return 0;
}

在本例中,我们使用compare()函数来比较str1的第7个字符开始的5个字符是否等于str2。如果相等,则输出“String found!”,否则输出“String not found!”。

三、string截取指定字符串

C++ string类提供了substr()函数来截取一个字符串中的一部分作为新的字符串。使用方式如下:

#include <string>
#include <iostream>

using namespace std;

int main()
{
  string str1 = "Hello, World!";
  string str2 = str1.substr(7, 5);
  
  cout << "Substring is: " << str2 << endl;
  
  return 0;
}

在本例中,我们使用substr()函数来截取str1字符串中从第7个字符开始,长度为5个字符的子字符串,并将其赋值给str2。程序的输出结果为:

Substring is: World

四、string包含某个字符串

我们也可以使用string::find_first_of()函数来查找字符串中是否有某个字符或字符串存在。使用方法如下:

#include <string>
#include <iostream>

using namespace std;

int main()
{
  string str1 = "Hello, World!";
  
  if (str1.find_first_of("Zy") != string::npos)
  {
    cout << "Found it!" << endl;
  }
  else
  {
    cout << "Not found!" << endl;
  }
  
  return 0;
}

在本例中,我们使用find_first_of()函数来查找str1中是否包含“Zy”这两个字符中的任意一个字符,如果存在,则输出“Found it!”;否则输出“Not found!”。

五、string字符串截取指定内容

除了使用substr()函数来截取字符串以外,我们还可以使用string::erase()函数来删除一个字符串中的一部分内容。使用方法如下:

#include <string>
#include <iostream>

using namespace std;

int main()
{
  string str1 = "Hello, World!";
  
  str1.erase(0, 7);
  
  cout << "New string is: " << str1 << endl;
  
  return 0;
}

在本例中,我们使用erase()函数来删除str1字符串的前7个字符,将得到一个新的字符串。程序输出的结果为:

New string is: World!

六、string判断包含字符串

除了使用find()函数来判断字符串中是否包含某个子字符串以外,我们也可以使用find_first_of()函数来实现同样功能,例如:

#include <string>
#include <iostream>

using namespace std;

int main()
{
  string str1 = "Hello, World!";
  
  if (str1.find_first_of("World") != string::npos)
  {
    cout << "Found it!" << endl;
  }
  else
  {
    cout << "Not found!" << endl;
  }
  
  return 0;
}

在本例中,我们使用find_first_of()函数来查找字符串中是否包含“World”子字符串,如果存在,则输出“Found it!”;否则输出“Not found!”。

七、string是否包含某个字符

我们也可以使用find()函数来判断字符串中是否包含某个字符,例如:

#include <string>
#include <iostream>

using namespace std;

int main()
{
  string str1 = "Hello, World!";
  
  if (str1.find('W') != string::npos)
  {
    cout << "Found it!" << endl;
  }
  else
  {
    cout << "Not found!" << endl;
  }
  
  return 0;
}

在本例中,我们使用find()函数来查找字符串中是否包含字符“W”,如果存在,则输出“Found it!”;否则输出“Not found!”。

八、string包含字符串的位置

除了使用find()函数来查找字符串中子字符串的位置以外,我们还可以使用string::find_first_of()函数来查找字符串中特定字符的位置,例如:

#include <string>
#include <iostream>

using namespace std;

int main()
{
  string str1 = "Hello, World!";
  
  cout << "Position of 'W' is: " << str1.find_first_of('W') << endl;
  
  return 0;
}

在本例中,我们使用find_first_of()函数来查找字符串中字符“W”的位置,程序的输出结果为:

Position of 'W' is: 7