Find a arrayList value in a string without looping trough it?
Assume i have my code like this...
ArrayList test =newArrayList();
test.Add("Test1");
test.Add("Test2");
test.Add("Test3");
string s ="Kevin tells you, 'Test2'";
for (int i = 0; i < test.Count; i++)
{
if (s.Contains(test
.ToString()) ==true)
{
MessageBox.Show("Found string in Key " + i +"!");
}
}
Is it possible to do the same without having to loop (for function) the arrayList? Cuz the array in my program can be huge and looping will delay to much. Also need to have the key returned where the match is been found in the ArrayList.
Regards Kevin;
[1606 byte] By [
r0swell] at [2007-12-24]
Use a hash table to hold the information
Dictionary<string, string> thelist = new Dictionary<string,string>();
thelist.Add("Test1", "Kevin");
thelist.Add("Test2", "Mark");
...
if (thelist.Contains("Test2"))
MessageBox("Found");
Well the problem is the app is a log parser,
The string s = a line from a text/log file
and the ArrayList / HashTable = a list of triggers to look for in the log file
So the logfile string is a long line like
string s = "This is an really long file";
And the ArrayList
Array.Items.Add("long file");
And needs to check if an value of the arrayList contains in the logString and not the other way arround, Or am i'm missing somthing? =p Wich could be i'm still farly new to c# and i come from tcl/php scripting so its a huge diff there
Thnx
r0swell wrote: |
And needs to check if an value of the arrayList contains in the logString and not the other way arround.. |
|
Then change the if above to look at the values instead of the keys
if (theList.ContainsValue("Kevin"))
...do something....A hash table is a Key / Value arrangement. The dictionary object as shown is a type specific version of the Hashtable class but both perform the same operations for what you need.
Still confused,
Look at the following code...
Dictionary<string, string> thelist = new Dictionary<string,string>();
thelist.Add("Test1", "Kevin");
thelist.Add("Test2", "Mark");
string s = "hello my name is Kevin";
if (thelist.ContainsValue(s))
MessageBox.Show("Found");
Will never work cuz its checking if "hello my name is Kevin" is found in the HastTable.
What i'm trying to do is to check if an value in the HashTable exists in the string "hello my name is Kevin"
without needing to loop trough the hashTable.
Sorry i prolly explained it wrong the first times. With my english being a bit weird etc,
I understand what you want now...to achieve the goal see this following code
| |
using System.Collections.Generic;.... Dictionary<string, int> theList = new Dictionary<string, int>(); theList.Add("Test1", 0); theList.Add("Test2", 0); theList.Add("Test3", 0); string line = @"Kevin tells you, 'Test2'"; string[] Tokens = line.Split(new char[3] { ' ', '\'', ',' }); foreach (string Tok in Tokens) { if (theList.ContainsKey(Tok)) { theList[Tok] = theList[Tok] + 1; Console.WriteLine(string.Format("{0} found current value is {1}", Tok, theList[Tok])); } } // Summary Console.WriteLine("{0}Summary", System.Environment.NewLine); foreach (KeyValuePair<string, int> entry in theList) Console.WriteLine("(({0}): {1})\t", entry.Key, entry.Value);
|
It prints out thisTest2 found current value is 1
Summary
((Test1): 0)
((Test2): 1)
((Test3): 0)