Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String is not splitting correctly

I am trying to split a string into a string[] made of the words the string originally held using the fallowing code.

    private string[] ConvertWordsFromFile(String NewFileText)
    {
        char[] delimiterChars = { ' ', ',', '.', ':', '/', '|', '<' , '>','/','@','#','$','%','^','&','*','"','(',')',';'};       
        string[] words = NewFileText.Split(delimiterChars);
        return words;


    }

I am then using this to add the words to a dictionary that keeps up with word keys and their frequency value. All other duplicated words are not added as keys and only the value is affected. However the last word is counted as a different word and is therefore made into a new key. How can i fix this?

This is the code I have for adding words to the dictionary :

public void AddWord(String newWord)
    {
       newWord = newWord.ToLower();
        try
        {
            MyWords.Add(newWord, 1);
        }
        catch (ArgumentException)
        {
            MyWords[newWord]++;
        }

    }

To clarify the problem i am having is that even if the word at the end of a string is a duplicate it is still treated like a new word and therefore a new string.

like image 892
James Thompson Avatar asked Aug 12 '26 23:08

James Thompson


2 Answers

Random guess - space at the end makes empty word that you don't expect. If yes - use correct option for Split:

var words = newFileText.Split(delimiterChars,
   StringSplitOptions.RemoveEmptyEntries);
like image 168
Alexei Levenkov Avatar answered Aug 14 '26 13:08

Alexei Levenkov


Split is not the best choice to do what you want to do because you end having this kind of problems and you also have to specify all the delimiters, etc.

A much better option is using a regular expressions instead of your ConvertWordsFromFile method as follow:

Regex.Split(theTextToBeSplitted, @"\W+")

This line will return an array containing all the 'words'. Once you have that, the next step should be create your dictionary so, if you can use linq in your code, the easiest and cleaner way to do what you want is this one:

var theTextToBeSplitted = "#Hi, this is a 'little' test: <I hope it is useful>";
var myDictionary = Regex.Split(theTextToBeSplitted, @"\W+")
                        .GroupBy(x => x)
                        .ToDictionary(x => x.Key, x => x.Count());

That´s all that you need.

Good luck!

like image 36
lontivero Avatar answered Aug 14 '26 14:08

lontivero