How to remove index numbers from each line of text file

Discussion in 'C#' started by near, Aug 28, 2012.

  1. near

    near New Member

    Joined:
    Aug 28, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I have a text file in which there are 10000 lines. There are 225 numerical values on each line and each numerical value is followed by a index number and a colon e.g., 1:0.021354 2:0.125432 3:451321 ...... 225:0.001254.
    Now I want to remove this indexing. I know how to add indexing but could not perform to remove that. Please help me in solving this problem. Thanks
    Here is the code for adding indexing.
    Code:
    static void Main(string[] args)
            {
                string[] files = Directory.GetFiles(@"F:\New folder", "*.txt", SearchOption.AllDirectories);
    
                StringBuilder strFile = new StringBuilder();
    
                foreach (string file in files)
                {
                    using (StreamReader sr = new StreamReader(file))
                    {
                        string s = Path.ChangeExtension(file, null);
                        strFile.AppendFormat(s.Substring(s.LastIndexOf(@"\") + 1) + " ");
                  //      string s = Path.GetFileName("-1");
                  //      strFile.AppendFormat(s + " ");
                        char[] charsToTrim = { ' ', '\r', '\n' };
                        string kkj = (sr.ReadToEnd().TrimEnd(charsToTrim));
                        string[] words = kkj.Split(' ');
                        string temp = "";
    
                        int iCounter = 1;
    
                        foreach (string word in words)
                        {
                            if (word.Contains("-0.000000"))
                            {
                                temp = word.Replace("-0.000000", "");
    
                                strFile.Append(temp);
    
                                iCounter++;
                                continue;
                            }
    
                            else if (word.Contains("0.000000"))
                            {
                                temp = word.Replace("0.000000", "");
    
    
                                strFile.Append(temp);
    
                                iCounter++;
                                continue;
                            }
    
                            strFile.Append(iCounter++ + ":" + word + " ");
    
                        }
                        strFile.AppendLine();
                    }
                }
                using (StreamWriter outfile = new StreamWriter(@"F:\New folder (3)\-1.train"))
                {
                    outfile.Write(strFile.ToString());
                }
            }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice