I have a String that I want to split by the whitespaces to store in a dictionary of words (simple enough). However, I also want each of word's index and length.
So far, I just have a Dictionary of the words and in which order they were found....
private Dictionary<int,String> makeDictionary(String ASCII)
{
string[] t = ASCII.Split(new[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
Dictionary<int, string> aDictionary = new Dictionary<int, string>();
for (int i = 0; i < t.Length; i++)
{
t[i] = stripSymbolsFromString(t[i]);
if (!aDictionary.ContainsValue(t[i]) && t[i] != "")
{
aDictionary.Add(i, t[i]);
}
}
return aDictionary;
}
Does anyone have any idea how I can use .Split() while keeping the indexes, or will I have to use a different technique of concatenation? As someone posted below, Using Regex will give the index of the match.
EDIT: I do not need the length. As someone pointed out, I can just get it from the string. I will just need the starting index of the word.
EDIT2: I will ignore duplicate words.
EDIT3: Here is an example of a string I would be using:
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
So the first couple elements would be
[0]=>Lorum,
[6]=>Ipsum,
[12]=>is
where the number 0,6,12 is the original index of the word within the String
Ordinal Position
when the strings are being split..? also if you can paste an example of the string you are try ing to split.. – MethodMan.ContainsValue
in every iteration. – Matt BurlandMatt
the dictionary should beDictionary<string, int> aDictionary = new Dictionary<string, int>();
– MethodMan