读取std :: string,从std :: string中删除所有特殊字符

时间:2022-08-22 13:04:01

I am very new to this forum and c++. So pardon me for my doubts/ questions. I am trying to read a std::string. I know I can access the elements using at or [int] operator. I've 2 questions:

我对这个论坛和c ++很新。所以请原谅我的疑惑/问题。我正在尝试读取一个std :: string。我知道我可以使用at或[int]运算符访问元素。我有两个问题:

1) remove or erase all special characters from string (includes spaces)

1)从字符串中删除或删除所有特殊字符(包括空格)

2) read only first 4 characters or letters from this string

2)只读取该字符串中的前4个字符或字母

For 1), I am checking on std::erase and std::remove_ifbut I need to eliminate all I mean special characters and spaces too. This means I need to include all the conditions that isspace()/ isalpha() and so on. Is there no single method to remove all at once?

对于1),我正在检查std :: erase和std :: remove_if但我需要消除所有我的意思是特殊字符和空格。这意味着我需要包含isspace()/ isalpha()等所有条件。有没有一种方法可以一次性删除所有方法?

For 2), I can access the string like an array, I mean string[0], string[1], string[2], string[3]. But I can't add this into single string?

对于2),我可以像数组一样访问字符串,我的意思是字符串[0],字符串[1],字符串[2],字符串[3]。但我不能将其添加到单个字符串中?

Please let me know how can I achieve this?

请让我知道如何实现这一目标?

3 个解决方案

#1


4  

To get the first four characters:

要获得前四个字符:

std::string first4=str.substr(0, 4);

To remove anything that isspace and isalpha predicates (although I think I misunderstood, here, do you mean isspace and is not isalpha??):

要删除任何isspace和isalpha谓词(虽然我认为我误解了,这里,你的意思是isspace而不是isalpha ??):

str.erase(std::remove_if(str.begin(), str.end(),
    [](char c) { return std::isspace(c) || std::isalpha(c); } ),
    str.end());

You can append to the string using op+=. For example:

您可以使用op + =附加到字符串。例如:

str+="hello";
str+='c';

#2


0  

To remove all special characters, why not make a method like so:

要删除所有特殊字符,为什么不制作这样的方法:

bool isLegal(char c)
{
  char legal[] = {'a', 'A', 'b','B' /*..and so on*/};
  int len = sizeof(legal)/sizeof(char);

  for (int i = 0; i < len; i++)
    if (c == legal[i])
      return true;
  return false;
}

and then just iterate trough the string and remove the characters not legal?

然后只是通过字符串迭代并删除不合法的字符?

#3


0  

For 1: std::remove_if will remove all elements for which the given predicate returns true. You provide the predicate function object, and it can be whatever you want it to be so long as it takes an element from your container (char) and returns a bool.

对于1:std :: remove_if将删除给定谓词返回true的所有元素。你提供谓词函数对象,它可以是你想要它的任何东西,只要它从你的容器(char)中获取一个元素并返回一个bool。

So you can write a function such as:

所以你可以写一个函数,如:

bool IsNotLegal(const char & stringElement);

or you can write it as a lambda function, and then you can pass it to std::remove_if to remove everything from the string that satisfies your conditions in 1 pass.

或者你可以把它写成一个lambda函数,然后你可以将它传递给std :: remove_if来从字符串中删除满足你的条件的所有内容。

std::string myString{"This is my string."};
std::remove_if(std::begin(myString), std::end(myString),[](const char & element)
{
    return std::isspace(element) && //any other conditions such as your own IsSpecial();
});
// now myString has become "Thisismystring."

#1


4  

To get the first four characters:

要获得前四个字符:

std::string first4=str.substr(0, 4);

To remove anything that isspace and isalpha predicates (although I think I misunderstood, here, do you mean isspace and is not isalpha??):

要删除任何isspace和isalpha谓词(虽然我认为我误解了,这里,你的意思是isspace而不是isalpha ??):

str.erase(std::remove_if(str.begin(), str.end(),
    [](char c) { return std::isspace(c) || std::isalpha(c); } ),
    str.end());

You can append to the string using op+=. For example:

您可以使用op + =附加到字符串。例如:

str+="hello";
str+='c';

#2


0  

To remove all special characters, why not make a method like so:

要删除所有特殊字符,为什么不制作这样的方法:

bool isLegal(char c)
{
  char legal[] = {'a', 'A', 'b','B' /*..and so on*/};
  int len = sizeof(legal)/sizeof(char);

  for (int i = 0; i < len; i++)
    if (c == legal[i])
      return true;
  return false;
}

and then just iterate trough the string and remove the characters not legal?

然后只是通过字符串迭代并删除不合法的字符?

#3


0  

For 1: std::remove_if will remove all elements for which the given predicate returns true. You provide the predicate function object, and it can be whatever you want it to be so long as it takes an element from your container (char) and returns a bool.

对于1:std :: remove_if将删除给定谓词返回true的所有元素。你提供谓词函数对象,它可以是你想要它的任何东西,只要它从你的容器(char)中获取一个元素并返回一个bool。

So you can write a function such as:

所以你可以写一个函数,如:

bool IsNotLegal(const char & stringElement);

or you can write it as a lambda function, and then you can pass it to std::remove_if to remove everything from the string that satisfies your conditions in 1 pass.

或者你可以把它写成一个lambda函数,然后你可以将它传递给std :: remove_if来从字符串中删除满足你的条件的所有内容。

std::string myString{"This is my string."};
std::remove_if(std::begin(myString), std::end(myString),[](const char & element)
{
    return std::isspace(element) && //any other conditions such as your own IsSpecial();
});
// now myString has become "Thisismystring."