0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > String > ToCharArray() / Split() [csharpの真似]

Last updated at Posted at 2015-10-09
動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)

http://qiita.com/7of9/items/e03083db57cb5cb77ef8
にて muroさんに教えていただいたC#での ToCharArray() と Split()をC++ Builderでも実装できないか試した。

# include <memory>
static bool ToCharArray(String srcStr, TStringList *dstSL)
{
	if (dstSL == NULL) {
		return false;
	}

	String code;
	for(int idx=1; idx <= srcStr.Length(); idx++) {
		code = srcStr.SubString(idx, 1);
		dstSL->Add(code);
	}

	return true;
}

static bool Split(String srcStr, TStringList *sepSL, TStringList *dstSL)
{
	if (sepSL == NULL || dstSL == NULL) {
		return false;
	}

	String res = L"";
	String code;
	bool isDelimiter;

	for(int idx=1; idx <= srcStr.Length(); idx++) {
		code = srcStr.SubString(idx, 1);
		isDelimiter = false;
		for(int si=0; si < sepSL->Count; si++) {
			if (code == sepSL->Strings[si]) {
				isDelimiter = true;
				break;
			}
		}
		if (isDelimiter) {
			OutputDebugString(res.c_str());
			if (res.Length() > 0) {
				dstSL->Add(res);
				res = L"";
			}
		} else {
			res = res + code;
		}
	}

	if (res.Length() > 0) {
		dstSL->Add(res);
		OutputDebugString(res.c_str());
	}
	return true;
}


void __fastcall TForm1::Button2Click(TObject *Sender)
{
	String separators = L",{:}";
	String sample = L"{\"AAA\":{\"bbb\",\"ccc\"},\"ddd\"";

	std::unique_ptr<TStringList> separator(new TStringList);
	std::unique_ptr<TStringList> element(new TStringList);

	bool res;
	res = ToCharArray( separators, separator.get() );
	if (res == false) {
		return;
	}

	res = Split( sample, separator.get(), element.get() );
	if (res == false) {
		return;
	}

	for(int idx=0; idx < element->Count; idx++) {
        Memo1->Lines->Add(element->Strings[idx]);
    }
}
結果
"AAA"
"bbb"
"ccc"
"ddd"
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?