I need to read file and split string into hash table in C#.
For example;
1231231 Hi this is the first entry / hi this is the second entry
Currently, I'm reading the lines in the files line by line using a StreamReader
, and then I splitting every line to 3 different strings. For example, "1231231" to one string, then up to "/" sign to another string at last after "/" sign to another string.
1231231 will be the key of the hash table, and the others will be the values of the hash table. I'm stuck开发者_StackOverflow on this part.
Assuming you have fairly regular input set, you'll probably want to use a regular expression for this.
This pattern seems like what you want:
^(\d+)\s+([^/]+)\s+/\s+(.+)$
That would be:
^
: Anchor to start of string(\d+)
: One or more digits\s+
: 1 or more whitespace characters([^/]+)
: 1 or more characters which do not equal '/'\s+/\s+
: 1 or more whitespace characters plus 1 slash and 1 or more whitespace characters(.+)
: 1 or more of any character$
: Anchor to end of string
Using Bobby's regex..
static void Main(string[] args)
{
Hashtable hashtable = new Hashtable();
string[] fileLines = File.ReadAllLines(@"PATH\FILE.TXT");
foreach (string line in fileLines)
{
var match = Regex.Match(line, @"^(\d+)\s+([^/]+)\s+/\s+(.+)$");
hashtable.Add(match.Groups[0].ToString(), new string[] { match.Groups[1].ToString(), match.Groups[2].ToString() });
}
}
The hashtable values are inserted as a string array since the key has to be unique.
Could be more optimal, but it'll work:
char stringSplit = '/';
char keySplit = ' ';
Dictionary<string,string[]> dictionary = new Dictionary<string, string[]>(1000);
using(StreamReader sr = new StreamReader(@"c:\somefile.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
int keyIndex = line.IndexOf(keySplit);
string key = line.Substring(0, keyIndex);
string[] values = line.Substring(keyIndex + 1).Split(stringSplit);
dictionary.Add(key,values);
}
}
Code to add entries to the hashtable:
Hashtable hashtable = new Hashtable(new EqualityComparer());
string[] fileLines = File.ReadAllLines(@"somePath");
foreach (var fileLine in fileLines)
{
int indexOfSpace = fileLine.IndexOf(' ');
int indexOfSlash = fileLine.IndexOf('/');
string keyString = fileLine.Remove(indexOfSpace);
string firstValue =
fileLine.Substring(indexOfSpace, indexOfSlash - indexOfSpace - 1);
string secondValue = fileLine.Substring(indexOfSlash + 1);
hashtable.Add(new Key(keyString), firstValue);
hashtable.Add(new Key(keyString), secondValue);
}
Key class to wrap same string:
public class Key
{
private readonly string s;
public Key(string s)
{
this.s = s;
}
public string KeyString
{
get { return s; }
}
}
Equality comparer that supplies GetHashCode functionality in order to make two keys based on same string go to the same entry in hashtable:
public class EqualityComparer : IEqualityComparer
{
public bool Equals(object x, object y)
{
return ReferenceEquals(x, y);
}
public int GetHashCode(object obj)
{
return ((Key) obj).KeyString.GetHashCode();
}
}
精彩评论