Hi, I'm trying to figure out c# for writing a method that searches a string array for each string that is in a different array of search terms.
Example :
string[] SearchInThisArray
string[] SearchForTheseStrings
I have string variables that are named the same as elements in SearchForTheseStrings[] that I wish to fill variable values from SearchInThisArray[]
How can this be done ? I have tried this following code which produces no errors and says all strings elements from SearchForTheseStrings are found in SearchThisArray, BUT I added a string to SearchForTheseStrings that I know is not in SearchInThisArray[] and it says its found ? Please help here is my attempt.
public SearchForTheseStrings()
{
string[] SearchForTheseStrings={"ex1", "ex2", "ex3", "ex4", "dinosaur"};
string[] SearchInThisArray={"ex1", "ex2", "ex3", "ex4", "ex5", "ex6", "ex7"};
}
// my actual code is this but above is example hopefully you understand my question
public SearchInArray()
{
string[] InfoProperties =
{
"ActivationState",
"BasebandSerial",
"BasebandVersion",
"BluetoothAddress",
"BrickState",
"BuildVersion",
"CpuArchitecture",
"CpuChipID",
"CpuChipSerial",
"DeviceClass",
"DeviceColor",
"DeviceID",
"DeviceName",
"HardwareModel",
"HostAttached",
"IMEI"
};
string[] ReadPropertiesList = File.ReadAllLines("C:\Properties.txt");
// produces a string array from properties.txt with values for each of the properties which i wish to store to c# string variables
for (int i = InfoProperties.GetLowerBound(0); i <= InfoProperties.GetUpperBound(0); i++)
Console.WriteLine(" [{0,2}]: {1}", i, InfoProperties[i]);
int index = Array.IndexOf(ReadPropertiesList, InfoProperties);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",ReadPropertiesList, index);
}

Ranganath PrasadPosted Jan 8, 2022, 6:23 PM
If i understood your requirement correctly ... for each element in the First array, try to find the FIRST matching element's index in the Second array.
You will have to make little modifications to your code. You can also use Linq ( as Sachin said ) or foreach, but that will just make code more readable and save you few keystrokes - Will not benefit much in terms of performance
Sachin SinghPosted Jan 8, 2022, 6:25 AM