strings

csharp Malawi
  • 9 years ago

     

    hi all, i have a problem, pls help me out. my problem is i have to read a file text file in which all words are seperated by commas. i hav to seperate those words and keep them in array. am able to trace each word in that file but not able to keep in arrays. The code i wrote for this is :

    FileStream fs;

    fs =

    new FileStream ( "input.txt", FileMode.Open); StreamReader sr= new StreamReader (fs );

     

    string str;

     

    while((str= sr.ReadLine ())!= null)

    {

     

    int i = str.IndexOf (",");

     

    string str1 = str.Substring (0,i);

     

    string s1 = str1;

     

    MessageBox.Show (s1);

     

     

    }

    Pls help me out...........

    \u003ctr\>\u003ctd\>\u003ctable cellspacing\u003d0 cellpadding\u003d0\>\u003ctr\>\u003ctd\>\u003cimg width\u003d16 height\u003d16 src\u003d\"/mail/images/txt.gif\"\>\u003ctd width\u003d7\>\u003ctd\>\u003cb\>input.txt\u003c/b\>\u003cbr\>1K Scanning for viruses...\u003c/table\>\u003c/table\>","116ab5ce81b21ca5"] ] ); D(["ce"]); //-->
  • 9 years ago
    You can use the String.Split method to split a string on a particular character or substring into an array.
  • 9 years ago

    I hav tried with that also but no use to me
  • 9 years ago

    In what way 'no use to you'?

    String.Split(,) takes the string, splits it up where it finds (in your case) a comma, and puts the results into an array.

    That's exactly what you asked for, and it does work.

    So when you tried it, what did your code look like, and what made you think it was not working?

     

  • 9 years ago

    actually my problem is i have to split a text file by commas and store that it into different array. By using split am able to store entire thing in one array.My input text file is something is like this.........................

    Company,contact,age,sex

    oracle,,john,24,m

    IBM, Luci,22,f

    Microsoft,Mallesh,26,m

    Satyam,rao,25,m

    Ibm,rosy,22,f  this is how my input.txt file looks like.now i have to seperate each word by comma ans store all company names in a array, contact names in another so  on.................... The code i wrote for this is like this..

    FileStream fs;

    fs =
    new FileStream ( "input.txt", FileMode.Open);

    StreamReader sr= new StreamReader (fs );

    string str;while((str= sr.ReadLine ())!= null)

    {

    int i = str.IndexOf (",");

    char[] ch = {','};

    string[] comp = str.Split (ch,i);

    foreach(string s1 in comp)

    {

    MessageBox.Show (s1);

    }

    }

    pls any1 help me... I need it urgently....

  • 9 years ago

    make use of arrays of arraylist

    StreamReader srObject = new StreamReader(sFilePath);

    string sCheckData;

    int iBound=0;

    ArrayList[] alData=null;

    while ((sCheckData = srObject.ReadLine()) != null)

    {

    string[] sData = sCheckData.Split(new char[] { ',' });Console.WriteLine(sCheckData);

    iBound = sData.Length;

    if(alData==null)

                   alData = new ArrayList[iBound];

    for (int iLoop = 0; iLoop < iBound; iLoop++)

    {

    if (alData[iLoop] == null)

             alData[iLoop] = new ArrayList();

    alData[iLoop].Add(sData[iLoop]);

    }

    }

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An expert is a man who has made all the mistakes that can be made in a very narrow field” - Niels Bohr