[英]How to convert a TStringDynArray to a TStringList
I'm using TDirectory::GetFiles()
to get a list of files (obviously). 我正在使用TDirectory::GetFiles()
来获取文件列表(显然)。 The result is stored in a TStringDynArray
and I want to transfer it to a TStringList
for the sole purpose to use the IndexOf()
member to see if a string is present in the list or not. 结果存储在TStringDynArray
,我想将其传递给TStringList
,其唯一目的是使用IndexOf()
成员查看列表中是否存在字符串。
Any solution that will let me know if a certain string is present in the list of files returned from TDirectory::GetFiles() will do fine. 如果从TDirectory :: GetFiles()返回的文件列表中存在某个字符串,任何能让我知道的解决方案都可以。 Although, it would be interesting to know how to convert the TStringDynArray. 虽然,知道如何转换TStringDynArray会很有趣。
TStringDynArray DynFiles = TDirectory::GetFiles("Foo path");
System::Classes::TStringList *Files = new System::Classes::TStringList;
Files->Assing(DynFiles) // I know this is wrong, but it illustrates what I want to do.
if(Files->IndexOf("Bar") { // <---- This is my goal, to find "Bar" in the list of files.
}
TStringList
and TStringDynArray
do not know anything about each other, so you will have to copy the strings manually: TStringList
和TStringDynArray
对彼此TStringDynArray
,因此您必须手动复制字符串:
TStringDynArray DynFiles = TDirectory::GetFiles("Foo path");
System::Classes::TStringList *Files = new System::Classes::TStringList;
for (int I = DynFiles.Low; I <= DynFiles.High; ++I)
Files->Add(DynFiles[I]);
if (Files->IndexOf("Bar")
{
//...
}
delete Files;
Since you have to manually loop through the array anyway, you can get rid of the TStringList
: 既然你必须手动遍历数组,你可以摆脱TStringList
:
TStringDynArray DynFiles = TDirectory::GetFiles("Foo path");
for (int I = DynFiles.Low; I <= DynFiles.High; ++I)
{
if (DynFiles[I] == "Bar")
{
//...
break;
}
}
But, if you are only interested in checking for the existence of a specific file, look at TFile::Exists()
instead, or even Sysutils::FileExists()
. 但是,如果您只想检查特定文件是否存在,请查看TFile::Exists()
,甚至是Sysutils::FileExists()
。
if (TFile::Exists("Foo path\\Bar"))
{
//...
}
if (FileExists("Foo path\\Bar"))
{
//...
}
* personally, I hate that the IOUtils
unit uses dynamic arrays for lists. *个人而言,我讨厌IOUtils
单元使用动态数组列表。 They are slow, inefficient, and do not integrate well with the rest of the RTL. 它们速度慢,效率低,并且不能与RTL的其余部分很好地集成。 But that is just my opinion. 但这只是我的观点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.