Learn how to #search for a #String inside a string, #list, #vector, #map and #dictionary using #find and #contains keywords in c plus plus and c sharp.
c++ code:
c# code:
using System;//for Console
using System.Collections.Generic;//for lists
using System.IO;//to use StreamReader
namespace lec2csharp
{
class Program
{
static void Main(string[] args)
{
StreamReader input_file = new StreamReader("test.txt");
StreamWriter output_file = new StreamWriter("test.txt.out");
string line = "";
//for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
//{
// bool search = line.Contains(".");
// //bool search = line.EndsWith(".");
// if (search != false)
// Console.WriteLine("element found at pos: {0} in line {1} line_no is {2}",
// line.IndexOf("."), line, line_no);
// Console.WriteLine("LINE {0} :{1}", line_no, line);
// output_file.WriteLine("LINE {0} :{1}", line_no, line);
//}
//List<string> elements = new List<string>();
//for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
//{
// elements.Add(line);
//}
//foreach (string iter in elements)
//{
// //bool search = iter.Contains(".");
// bool search = iter.EndsWith(".");
// if (search != false)
// Console.WriteLine("element found at pos {0} in line {1}",
// iter.IndexOf("."), iter);
// Console.WriteLine(iter);
// output_file.WriteLine(iter);
//}
//string[] elements = File.ReadAllLines("test.txt");
//int count = 0;
//for (int line_no = 0; (line = input_file.ReadLine()) != null; ++line_no)
//{
// elements[line_no] = line;
// count++;
//}
//for (int line_no = 0; line_no < count; ++line_no)
//{
// //bool search = elements[line_no].Contains(".");
// bool search = elements[line_no].EndsWith(".");
// if (search != false)
// Console.WriteLine("element found at pos {0} in line {1}",
// elements[line_no].IndexOf("."), elements[line_no]);
// Console.WriteLine(elements[line_no]);
// output_file.WriteLine(elements[line_no]);
//}
Dictionary<String, int> elements = new Dictionary<string, int>();
for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
{
elements.Add(line, line_no);
}
foreach (KeyValuePair<string, int> iter in elements)
{
bool search = iter.Key.Contains(".");
//bool search = iter.Key.EndsWith(".");
if (search != false)
Console.WriteLine("element is at pos: {0} found in line {1} line_no is: {2}",
iter.Key.IndexOf("."), iter.Key, iter.Value);
Console.WriteLine("LINE {0} :{1}", iter.Value, iter.Key);
output_file.WriteLine("LINE {0} :{1}", iter.Value, iter.Key);
}
if (input_file != null)
{
input_file.Close();
output_file.Close();
}
Console.ReadLine();
}
}
}
#cplusplus #csharp #tutorial
Searching characters within a string c#
How to Find String in C# Csharp
C# List Searching and Finding