从 S 中删除'red'组合的第一个匹配项

Delete from S first occurrence of a combination of 'red'

本文关键字:第一个 组合 red 删除      更新时间:2023-10-16

我在将代码重写为Builder c++6格式时遇到问题。因此任务如下:

  1. 从S中删除第一个出现的'red'组合
  2. 在第一个组合"th"之后粘贴"e">
  3. 将5个符号从S复制到x20,并将其粘贴到第6个成员之后(解决问题)
  4. 删除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;
    }
    

您正在将AnsiStringstd::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;
}
相关文章: