从 S 中删除'red'组合的第一个匹配项
Delete from S first occurrence of a combination of 'red'
我在将代码重写为Builder c++6格式时遇到问题。因此任务如下:
- 从S中删除第一个出现的'red'组合
- 在第一个组合"th"之后粘贴"e">
- 将5个符号从S复制到x20,并将其粘贴到第6个成员之后(解决问题)
-
删除S 中的所有"."answers",">
void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString S = Edit1->Text; AnsiString X = Edit2->Text; string str; //Delete from S first occurrence of a combination of 'red' str = "red"; std::size_t pos = S.find(str); if(pos != std::string::npos){ S.replace(pos, str.length(), ""); } //After first combination 'th' paste 'e' str = "th"; pos = S.find(str); if(pos != std::string::npos){ S.insert(pos + str.length(), "e"); } //Copy 5 symbols to Х from S and paste them after the 6th member str = 6; pos = S.find(str); if(pos != std::string::npos){ X = S.substr(pos + str.length(), 5); } //Delete all points and comas for(int i=1;i<s.Length();i++){ if(s[i]=='.')s.Delete(i,1); } for(int i=1;i<s.Length();i++){ if(s[i]==',')s.Delete(i,1); } Label1->Caption=S; Label2->Caption=X; }
您正在将AnsiString
和std::string
逻辑混合在一起(或者您可能正在从std::string
迁移,但不知道如何为AnsiString
重写?)。find()
、replace()
、insert()
、length()
和substr()
都是std::string
方法。AnsiString
的等价物是Pos()
、Delete()
、Insert()
、Length()
和SubString()
。
没有理由在同一个函数中将这两种字符串类型混合在一起。选择其中一个。
此外,删除句点/逗号的两个循环也被打断了。您忽略了字符串中的最后一个字符,并且每次删除字符时都会跳过一个字符。因此,要么修复循环,要么用C++Builder的StringReplace()
函数替换它们。
如果您想重用现有的基于std::string
的代码,您可以这样做。您不必使用AnsiString
:
#include <string>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
std::string S = Edit1->Text.c_str();
std::string X = Edit2->Text.c_str();
std::string str;
//Delete from S first occurrence of a combination of 'red'
str = "red";
std::size_t pos = S.find(str);
if (pos != std::string::npos){
S.replace(pos, str.length(), "");
}
//After first combination 'th' paste 'e'
str = "th";
pos = S.find(str);
if (pos != std::string::npos){
S.insert(pos + str.length(), "e");
}
//Copy 5 symbols to Х from S and paste them after the 6th member
str = "6";
pos = S.find(str);
if (pos != std::string::npos){
X = S.substr(pos + str.length(), 5);
}
//Delete all points and comas
pos = S.find_first_of(".,");
while (pos != std::string::npos) {
s.erase(pos, 1);
pos = S.find_first_of(".,", pos);
}
Label1->Caption = S.c_str();
Label2->Caption = X.c_str();
}
然而,由于您正在与VCL组件交互,因此重写代码以使用AnsiString
(或者更好的System::String
,以防您将代码迁移到现代C++Builder版本)可能是有意义的:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
System::String S = Edit1->Text;
System::String X = Edit2->Text;
System::String str;
//Delete from S first occurrence of a combination of 'red'
str = "red";
int pos = S.Pos(str);
if (pos != 0) {
S.Delete(pos, str.Length());
}
//After first combination 'th' paste 'e'
str = "th";
pos = S.Pos(str);
if (pos != 0) {
S.Insert(pos + str.Length(), "e");
}
//Copy 5 symbols to Х from S and paste them after the 6th member
str = 6;
pos = S.Pos(str);
if (pos != 0) {
X = S.SubString(pos + str.Length(), 5);
}
//Delete all points and comas
S = StringReplace(S, ".", "", TReplaceFlags() << rfReplaceAll);
S = StringReplace(S, ",", "", TReplaceFlags() << rfReplaceAll);
Label1->Caption = S;
Label2->Caption = X;
}
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 如何在创建自定义迭代器时获得 std::p air 的第一个和第二个?
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- 使第二个类的构造函数成为第一个类中的友元函数
- 为什么第二个代码给出了预期的结果,而第一个代码却没有?
- 使用哈希图打印第一个重复元素;
- 从 S 中删除'red'组合的第一个匹配项
- 如何组合两个查询并使用第一个查询的结果作为第二个查询的输出
- 如何使用组合键获取boost::multi_index_container中第一个键的不同计数