Hey,

I'm trying to search a List for a substring, but even if it does exist the result is coming out as negative (not present) using the BinarySearch.

The idea is that once the file has been loaded into the list I search to see if substrings exist (i.e. IDS_STRING1 ) <-- includes tab. The lines in the file will say "IDS_STRING# "Something else written after tab"", but as anything could be written after the tab, that's why I need to search for the first part of the string only.

The text file is in Unicode (UTF-16 LE).

Can anyone see what I'm doing wrong in the code below? Sorry if it's really obvious, but I just can't see it.

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"\x09";
	wstring thisString = integer.str();
	String^ SpareString = gcnew String (thisString.c_str());
	int index = load->BinarySearch(SpareString);
	System::IO::StreamWriter^ WriteSpareStrings = gcnew System::IO::StreamWriter(L"Chinese.txt", true, System::Text::Encoding::Unicode);
	if(index < 0)
	{
		WriteSpareStrings->Write(SpareString + L"\"Spare String\"");
		WriteSpareStrings->Write(L"\r\n");
	}
	WriteSpareStrings->Flush();
	WriteSpareStrings->Close();
}

Thanks!

Recommended Answers

All 2 Replies

Use a "for each" on the list and use ->Contains() on each string as it is looped.

Hey, thanks for your suggestion, I actually solved it yesterday by using a Dictionary and splitting the strings.

Dictionary<String^,String^>^ load = gcnew Dictionary<String^,String^>();
// load file
{
	System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(L"Chinese.txt",System::Text::Encoding::Unicode,true);
	String^ line1;
	array<System::String^>^ split = gcnew array<System::String^>(2);
	while((line1 = sr->ReadLine()) !=nullptr)
	{
		split = line1->Split(L'\t');
		String^ key = split[0];
		if(key->StartsWith(L"IDS"))
		{
			String^ value = split[1];
			load->Add(key,value);
		}
	}
	sr->Close();
}

// output to file
for(int i = 1; i < 1018; ++i)
{
	std::wostringstream integer;
	integer << L"IDS_STRING" << i;
	wstring thisString = integer.str();
	String^ SpareString = gcnew String (thisString.c_str());
	System::IO::StreamWriter^ WriteSpareStrings = gcnew System::IO::StreamWriter(L"Chinese.txt", true, System::Text::Encoding::Unicode);
	if(load->ContainsKey(SpareString)==false)
	{
		WriteSpareStrings->Write(SpareString + L"\t\"Spare String\"");
		WriteSpareStrings->Write(L"\r\n");
	}
	WriteSpareStrings->Flush();
	WriteSpareStrings->Close();
}
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.