LoginSignup
0
1

More than 5 years have passed since last update.

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

Posted at
動作環境
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;
}
//---------------------------------------------------------------------------

実行例

2019-05-24_13h53_12.png

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

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1