[英]How can I delete first and last characters of strings using member functions of std::string, without using remove_if, erase, etc?
编辑:该问题已于 2020 年 10 月 10 日下午 1 点解决。谢谢!
我想知道如何删除字符串变量“words”的第一个和最后一个特殊字符,而不触及中间的特殊字符并使用 remove 和 erase-remove_if 习语。 例如,字符串 @* Case-closed ,。 应返回 Case-closed,但不得触摸内部特殊字符。
具体来说,我想利用 find_first_of、find_last_of、length() [相当于 size()]。 这是我尝试的代码,但我不确定如何 go 为 for 循环(或 while 循环)设置 boolean 条件。 下面的代码是我的尝试:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main(){
const string charsToSearchFor = "!@#$%^&*()_-+={}[]:;\"\'`<>,.\?/|\\";
string words = "@**Man_Of_Steel...!";
string forDisplay;
int length = static_cast<int>(words.length()) - 1;
cout<<"The string is "<<length<<" characters long."<<endl;
int count1 = 0;
int count2 = 0;
bool foundOnFirst((int)words.find_first_of(charsToSearchFor) == 0);
bool foundOnLast((int)words.find_last_of(charsToSearchFor) == length);
bool isAlphabet((words[length] >= 'A' && words[length] <= 'Z') || (words[length] >= 'a' && words[length] <= 'z' ));
cout<<"The first instance was found on "
<<(int)words.find_first_of(charsToSearchFor)
<<endl;
cout<<"The last instance was found on "
<<(int)words.find_last_of(charsToSearchFor)
<<endl;
cout<<"The boolean variable foundOnFirst should be true; actually it is: "
<<foundOnFirst
<<endl;
cout<<"The boolean variable foundOnLast should be true; actually it is: "
<<foundOnLast
<<endl;
cout<<"This should be one; returns: "<<foundOnFirst<<std::endl;
cout<<"This should be one; returns: "<<foundOnLast<<std::endl<<std::endl;
//for deleting the first few special characters of a string; I have no clue
//what Boolean condition would go into the second part of the for loop
if (!isAlphabet || foundOnLast) {
for (length = words.length(); !isAlphabet; length--) {
//forDisplay = words.substr(1, length + 1);
cout<<"The word in the loop process is: "<<forDisplay<<endl;
cout<<"The length currently is: "<<length<<endl;
forDisplay = words.substr(1, length + 1);
count2++;
}//end if
}
//for deleting the last few special characters of a string; again, I have no
//idea what Boolean condition would go into the second part of the for loop
if (!isAlphabet || foundOnLast) {
for (length = words.length(); !isAlphabet; length--) {
//forDisplay = words.substr(1, length + 1);
cout<<"The word in the loop process is: "<<forDisplay<<endl;
cout<<"The length currently is: "<<length<<endl;
forDisplay = words.substr(1, length + 1);
count2++;
}//end if
}
cout<<"The word without the special characters is "<<forDisplay<<endl;
return 0;
}
你的建议是什么? 非常感谢。
你可以像这样用 substr function 解决你的问题:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s = "substring";
string d = s.substr(1,s.size()-2);
cout<<d;
return 0;
}
第一个参数是你想要开始的索引,第二个参数是新字符串的长度所以这里新字符串从 s 中的第二个字母开始并且它需要(s 的大小)-2 所以 output 是“ ubstrin" 我希望这是有用的
祝你好运!
您可以将find_first_of
与find_first_not_of
结合使用以删除第一个特殊字符,将find_last_of
与find_last_not_of
结合使用以删除最后一个特殊字符。
或者,如果你想删除直到字母字符,你可以做一个循环来搜索第一个和最后一个字母字符,然后substr
该范围。
无论哪种方式,都可以考虑仅使用 1 个标准、字母表或黑名单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.