Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

How to add strings to a new file ?

$
0
0

This is the code:

private void InsertContent()
        {
            StreamReader r = new StreamReader(localTextFile);
            StreamWriter w = new StreamWriter(@"D:\localscrambledfile.txt");
            string f = r.ReadToEnd();
            int index = 0;
            int nextIndex = 0;
            string firstWord = "";
            for (int i = 0; i < originalWords.Count; i++)
            {
                firstWord = originalWords[i];
                string startTag = firstWord;
                int startTagWidth = startTag.Length;
                    index = f.IndexOf(firstWord, index);
                    if (index == -1)
                    {
                        break;
                    }
                    string g = f.Substring(nextIndex, index);
                    w.Write(g);
                    w.Write(scrambledWords[i]);
                    nextIndex = index + startTagWidth;
            }
            w.Close();
        }


originalWords is List<string> and also scrambledWords is a List<string>

In the first loop nextIndex is 0 and index is 55 so i write the content of the original file(localTextfile) to the new file(localscrambledfile.txt)

Then i take the first string from the scrambledWords List and write it after i wrote the variable g content

Next loop the variable nextIndex is 59 and also the variable index is 59 

And the variable g is now length 73 : International - Breaking, World, Business, Sports, Entertai

But in fact there is no content between the first string from the List scrambledWords and the second string .

For example the first loop : nextIndex is 0 and index is 55 

The first string in the scrambledWords is "com " thats length of 4

Now in the original file after "com " there is no content but the next word "International "

There was a content before "com " but after it there is no content .

So the next loop i was suppose to get nothing and just add the next string from the List

So why in the second loop itertion g contain: International - Breaking, World, Business, Sports, Entertai ?

I don't get it.

This is how the original file content look like:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<title>CNN.com International - Breaking, World, Business, Sports, Entertainment and Video News</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/

So in the first loop im getting from 0 to 55 and write the word from the List scrambledWords

The word is "com "

The next itertion loop should be "International " thats also the string in the List

But g contain  International - Breaking, World, Business, Sports, Entertai

I should each loop itertion to get the next content if there is any and then add the next string from the List

I dont get it.


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>