从 S 中删除第一次出现的组合 'red'

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

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

  1. 从 S 中删除第一次出现的 'red'
  2. 的组合
  3. 第一次组合后'th'粘贴'e'
  4. 从S复制5个符号到Х,粘贴到第6个成员后(求解有问题)
  5. 删除所有“.”和“,”来自 S

    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()

没有理由在同一函数中将两种字符串类型混合在一起。选择一个或另一个。

此外,您要删除 periods/commas 的两个循环已损坏。您忽略了字符串中的最后一个字符,并且每次删除字符时都会跳过一个字符。所以,要么修复循环,要么你可以用 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;
}