Hi,

I want to search a managed list, List<String^>^ list, for a variable substring (i.e. the substring changes it's contents every time it is looped).

The list is populated by a Unicode (UTF-16LE) text file. The substring to be searched for is basically an ID for the Strings that occurs at the beginning of each String (the second half of the string is unknown), so I loop through and search the List for a different ID each time (i.e. IDS_STRING1, IDS_STRING2 etc.) The idea being that if it doesn't yet exist, I write it to file.

I can declare the List and store the file in it no problem, but when it comes to searching this managed list for a managed substring, I just can't seem to figure it out. What is the best method to use?

Here's my code so far (but remember, I don't know how to search for the substring, so I don't think my method is correct):

List<String^>^ load = gcnew List<String^>();
// load file
{
	System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(L"Chinese.txt",System::Text::Encoding::Unicode,true);
	String^ line1;
	while((line1 = sr->ReadLine()) !=nullptr)
	{
		load->Add(line1);
	}
	sr->Close();
	load->Sort();
}

// output to file
for(int i = 1; i < 1018; ++i)
{
	std::wostringstream integer;
	integer << L"IDS_STRING" << i << L"\t";
	wstring thisString = integer.str();
	String^ SpareString = gcnew String (thisString.c_str());
	bool index = load->StartsWith(SpareString);      //I know it's not StartsWith, this is just me putting something in to identify what needs doing
	System::IO::StreamWriter^ WriteSpareStrings = gcnew System::IO::StreamWriter(L"Chinese.txt", true, System::Text::Encoding::Unicode);
	if(index == false)
	{
		WriteSpareStrings->Write(SpareString + L"\"Spare String\"");
		WriteSpareStrings->Write(L"\r\n");
	}
	WriteSpareStrings->Flush();
	WriteSpareStrings->Close();
}

The other thing to note is that when I do the search it has to include the tab after the IDS_STRING# as I don't want to find IDS_STRING30 for IDS_STRING3 or something like that.

Any insight would be greatly appreciated, thank you.

Narrowing this down to the specific question, here is technique 1:

#include "stdafx.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Linq;

int main(array<System::String ^> ^args)
{
   // Just some list...
   List<String^>^ lst_strData =
      Enumerable::ToList<String^>(
         gcnew array<String^>{"New Mexico", "Paul Newman", "Noobie", "Spiro Agnew"});//whatever...

   for each(String^ str in lst_strData)
   {
      if(str->ToLower()->Contains("new"))
      {
         Console::WriteLine("Target: " + str);
      }
   }

   return 0;
}

Also in this particular case, I would not mix iostream and CLR stuff -- it's confusing.

Technique 2:

#include "stdafx.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Linq;

bool valueCheck(String^ str){return str->ToLower()->Contains("new");}
int main(array<System::String ^> ^args)
{
   List<String^>^ lst_strData =
      Enumerable::ToList<String^>(
         gcnew array<String^>{"New Mexico", "Paul Newman", "Noobie", "Spiro Agnew"});//whatever...

   Enumerable::ToList<String^>(
      Enumerable::Where(lst_strData, gcnew Func<String^, bool>(valueCheck)))
         ->ForEach(gcnew Action<String^>(Console::WriteLine));

   return 0;
}

ALSO, be careful because it looks like you're going to overwrite your output file 1017 times.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.