Click here to Skip to main content
16,021,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i read file in c# using streamreader.File.readallLine() method and every line contains three float values.How to extract every float value by splitting string?
My code is given below for split string but it not working proporly!
C#
try {
               
                var lines = File.ReadAllLines("InFIle.txt").Skip(1).ToList();
                for (int i = 0; i < lines.Count; i++)
                {
                    var line = lines[i];
                    var columns = line.Split('\n');
                }             

            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }


Received string in "-610\t -701\t -701" format,how to extract second and third column value?
Posted
Updated 21-Nov-13 0:55am
v2
Comments
Er Daljeet Singh 21-Nov-13 6:56am    
Provide the data that you have in file.
you said that every line has three float values i just want to how the values a represented in the string.
Thomas ktg 21-Nov-13 7:06am    
There is no floating points in the string received as you have mentioned. Show some data from the .txt file with the format.
Venkat Raghvan 21-Nov-13 7:39am    
sorry guys mine mistake i was confused with data in file format.I want to convert that data in double.The data is given below:
-610 -701 -701
-607 -699 -699
-606 -697 -697
-604 -696 -696
-603 -696 -695
-603 -696 -695
ZurdoDev 21-Nov-13 7:46am    
Just use the Split method on the String. Where are you stuck?

You can get decimal values like below(If your string will always like above)
C#
try
           {

               var lines = File.ReadAllLines("InFIle.txt").Skip(1).ToList();
               for (int i = 0; i < lines.Count; i++)
               {
                   var line = lines[i];
                   var columns = line.Split('\n');
                   for (int columnsLoop = 0; columnsLoop < columns.Length; columnsLoop++)
                   {
                       string[] DecimalsArray = columns[columnsLoop].Split('\t');
                       decimal firstVal = Convert.ToDecimal(DecimalsArray[0]);
                       decimal secondVal = Convert.ToDecimal(DecimalsArray[1]);
                       decimal thirdVal = Convert.ToDecimal(DecimalsArray[2]);
                   }

               }

           }
           catch (Exception exc)
           {
               MessageBox.Show(exc.Message);
           }
 
Share this answer
 
Comments
Venkat Raghvan 21-Nov-13 8:05am    
Thanks dude! it works fine.
Assuming that you want numbers like "-610" to actually be converted to the Float numeric type:
C#
// required
using System.Collections.Generic;
using System.Linq;

// inside your Class, Form, etc.
private string floatStr = @"-610	-701	-701
-607	-699	-699
-606	-697	-697
-604	-696	-696
-603	-696	-695
-603	-696	-695";

// split on tab character
private char[] splitChar = new char[] {'\t'};

// to hold result of split
private string[] stringAry;

// to hold list of float result
private List<float> floatList;

private void stringsToFloat (object sender, EventArgs e)
{
    // get rid of newline and linefeed
    floatStr = floatStr.Replace('\r', '\t');
    floatStr = floatStr.Replace('\n', '\t');

    // split
    stringAry = floatStr.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);

    // use Linq to convert to List<float>
    floatList = stringAry.Select(float.Parse).ToList();
}</float>
After this method is called, 'floatStr will contain a List of 18 float values.

Note that there's no validation here; what happens if there's an incorrect entry for one of the string to be converted to a float ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900