Jordan Moffat
Jordan Moffat

Reputation: 337

String Concatenation / Overwriting?

This is a program that reads in a CSV file, adds the values to a dictionary class and then analyses a string in a textbox to see if any of the words match the dictionary entry. It will replace abbreviations (LOL, ROFL etc) into their real words. It matches strings by splitting the inputted text into individual words.

 public void btnanalyze_Click(object sender, EventArgs e)
    {
        var abbrev = new Dictionary<string, string>();
        using (StreamReader reader = new StreamReader("C:/Users/Jordan Moffat/Desktop/coursework/textwords0.csv"))
        {
            string line;
            string[] row;

            while ((line = reader.ReadLine()) != null)
            {
                row = line.Split(',');

                abbrev.Add(row[0], row[1]);
                Console.WriteLine(abbrev);
            }
        }


        string twitterinput;
        twitterinput = "";
        // string output;
        twitterinput = txtInput.Text;
        {
            char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

            string text = twitterinput;

            string[] words = twitterinput.Split(delimiterChars);


         string merge;
            foreach (string s in words)
            {
                if (abbrev.ContainsKey(s))
                {
                    string value = abbrev[s];


                    merge = string.Join(" ", value);
                }

                    if (!abbrev.ContainsKey(s))
                    {
                        string not = s;
                        merge = string.Join(" ", not);
                    }

                        ;
                        MessageBox.Show(merge);

                }

The problem so far is that the final string is outputted into a text box, but only prints the last word as it overwrites. This is a University assignment, so I'm looking for a push in the correct direction as opposed to an actual answer. Many thanks!

Upvotes: 2

Views: 953

Answers (3)

Kishore Kumar
Kishore Kumar

Reputation: 12874

Better to User StringBuilder Class for such purpose

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

Upvotes: 0

ChrisF
ChrisF

Reputation: 137148

This is the problem:

string not = s;
merge = string.Join(" ", not);

You are just joining a single element (the latest) with a space delimiter, thus overwriting what you previously put into merge.

If you want to stick with string you need to use Concat to append the new word onto the output, though this will be slow as you are recreating the string each time. It will be more efficient to use StringBuilder to create the output.

If your assignment requires that you use Join to build up the output, then you'll need to replace the target words in the words array as you loop over them. However, for that you'll need to use some other looping mechanism than foreach as that doesn't let you modify the array you're looping over.

Upvotes: 0

svick
svick

Reputation: 244797

string.Join() takes a collection of strings, concatenates them together and returns the result. But in your case, the collection contains only one item: value, or not.

To make your code work, you could use something like:

merge = string.Join(" ", merge, value);

But because of the way strings work, this will be quite slow, so you should use StringBuilder instead.

Upvotes: 2

Related Questions