C++ Builder 10.2 Tokyo > CSV文字列 > (")を付加する関数 | (")を取り除く関数


動作環境
RAD Studio 10.2 Tokyo Update 3

概要

JSON文字列に文字列置換をしようとしたらはまってしまった。
"がDelimitedText使用時に消えるため。

それを回避するための実装を行った。

実装

今回はUnit1.cppにクラス(CUtilCsv_specialChar)を追加して実装した。

本来は1ファイルに複数のクラスを入れることはしない。
ソース分離する前段階の状況。

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TMemo *Memo1;
    TButton *Button1;
    void __fastcall Button1Click(TObject *Sender);
private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};

class CUtilCsv_specialChar
{
public:
    static String __fastcall putEachItemBetweenChars(String csvStr, char specialChar);
    static String __fastcall removeCharsFromEachItem(String csvStr, char specialChar);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <memory>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Memo1->Lines->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String srcstr = L"\"AAA\",\"BBB\",\"CCC\"";

    Memo1->Lines->Add(srcstr);

    String wrk = srcstr;  // 作業用

    wrk = CUtilCsv_specialChar::removeCharsFromEachItem(wrk, '"');
    Memo1->Lines->Add(wrk);

    wrk = CUtilCsv_specialChar::putEachItemBetweenChars(wrk, '"');
    Memo1->Lines->Add(wrk);
}

/*static*/String __fastcall CUtilCsv_specialChar::putEachItemBetweenChars(String csvStr, char specialChar)
{
    std::unique_ptr<TStringList> wrkSL(new TStringList);
    wrkSL->StrictDelimiter = true;
    wrkSL->Delimiter = ',';
    wrkSL->DelimitedText = csvStr;

    String res = L"";
    for(int idx=0; idx < wrkSL->Count; idx++) {
        if (res.Length() > 0) {
            res += L",";
        }
        res += String(specialChar) + wrkSL->Strings[idx] + String(specialChar);
    }
    return res;
}

/*static*/String __fastcall CUtilCsv_specialChar::removeCharsFromEachItem(String csvStr, char specialChar)
{
    String res = StringReplace(csvStr, String(specialChar), L"", TReplaceFlags()<<rfReplaceAll);
    return res;
}
//---------------------------------------------------------------------------

実行例

1行目: 元の文字列
2行目: double quotationを除去した後
3行目: double quotationを入れた後