14 Replies - 670 Views - Last Post: 24 April 2020 - 05:09 PM Rate Topic: -----

#1 Transistor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 20-November 13

Retrieve text from file with Split

Posted 23 April 2020 - 07:46 PM

Hi,

I've converted some VB code over to c# however I haven been
able to get this to work correctly in c#.

So in the resource folder I have a text file called
itemlist which contains lines of text just for an example-

itm1         "chocolate milk"
itm2         "frosted cake"
itm3         "super donuts "
.
.
.
.



I want it to search for the item on the left then retrieve
whats in quotes on the right.
It worked in VB but in c# with the following code if it
finds itm3 I'll get "super" but not "super donuts", same for the others.

Any ideas ?

Thanks. :)



private void loadItems(string name)
{
            string textline;
           
            int count = 0;

            string fileContent = Properties.Resources.itemlist;

            StringReader stringStream = new System.IO.StringReader(fileContent);


            int x;
            string itmName = "";

            using (TextFieldParser parser = new TextFieldParser(stringStream))
                do
                {

                    textline = parser.ReadLine();
                    string[] splitter = textline.Split();


                    if (name == splitter[0])
                    {
                        for (x = 1; x < splitter.GetUpperBound(0); x++)
                        {
                            if (splitter[x] == "")
                            { }
                            else
                            {
                               itmName = splitter[x];

                            }
                        }
                       itmName = itmName.Replace((char)34, (char)0);
                       item_Grid.Rows.Add();
                       item_Grid.Rows[item_Grid.RowCount - 1].Cells[0].Value = itmName;

                      
                        break;// found it now get out.
                    }




                   
                   
                }
                while (parser.EndOfData ==false);
}


This post has been edited by Transistor: 23 April 2020 - 07:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve text from file with Split

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15796
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Retrieve text from file with Split

Posted 23 April 2020 - 09:36 PM

If everything is a fixed amount of space between each column then cut the process. Replace the bulk of the spaces with a character that isn't used (like the # sign), and then split around that.
Was This Post Helpful? 0
  • +
  • -

#3 Transistor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 20-November 13

Re: Retrieve text from file with Split

Posted 24 April 2020 - 09:03 AM

Unfortunately they are not. There might be one space space between the two or a lot of space between the two.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15796
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Retrieve text from file with Split

Posted 24 April 2020 - 09:24 AM

Hmm. So not a tab delimited.

Mkay then alternatively I would look at splitting on the " . Sure a little clean up, but that should ensure you get both words in the quotes.

Barring that - there's always the joys of Regex!
Was This Post Helpful? 0
  • +
  • -

#5 DarenR   User is offline

  • D.I.C Lover

Reputation: 708
  • View blog
  • Posts: 4,580
  • Joined: 12-January 10

Re: Retrieve text from file with Split

Posted 24 April 2020 - 09:48 AM

cant you do a split the text and then trim the empty space? then you can do your search
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Retrieve text from file with Split

Posted 24 April 2020 - 09:52 AM

Print out the contents of splitter to see what you are dealing with, or step through the code (debug it) and examine the variable(s).
Was This Post Helpful? 0
  • +
  • -

#7 Transistor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 20-November 13

Re: Retrieve text from file with Split

Posted 24 April 2020 - 10:00 AM

View Postmodi123_1, on 24 April 2020 - 09:24 AM, said:

Hmm. So not a tab delimited.

Mkay then alternatively I would look at splitting on the " . Sure a little clean up, but that should ensure you get both words in the quotes.

Barring that - there's always the joys of Regex!


Ah I see.

Regex never used it.. might look into it later.
thanks.
Was This Post Helpful? 0
  • +
  • -

#8 Transistor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 20-November 13

Re: Retrieve text from file with Split

Posted 24 April 2020 - 10:12 AM

View Postandrewsw, on 24 April 2020 - 09:52 AM, said:

Print out the contents of splitter to see what you are dealing with, or step through the code (debug it) and examine the variable(s).

Did that and in the debugger and
with itmName = splitter[x];
splitter[10] would be the first text in the quotes
splitter[11] would be the next text in the quotes.

Just use to this working in VB. Rather new to c# and not sure why its working
differently in c#.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15796
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Retrieve text from file with Split

Posted 24 April 2020 - 10:20 AM

Do you have the VB code? They should function, roughly, the same.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Retrieve text from file with Split

Posted 24 April 2020 - 10:28 AM

This if (splitter[x] == ""), at least, is probably wrong.
Was This Post Helpful? 0
  • +
  • -

#11 Transistor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 20-November 13

Re: Retrieve text from file with Split

Posted 24 April 2020 - 10:33 AM

View Postmodi123_1, on 24 April 2020 - 10:20 AM, said:

Do you have the VB code? They should function, roughly, the same.


Yep. VB code -
Function loadItems(ByRef name As String)

        
        Dim splitter() As String
        Dim textline As String

 
        Dim count As Integer

        Dim fileContent As String = My.Resources.itemlist
        Dim stringStream As New System.IO.StringReader(fileContent)
        Dim x As Integer
        Using parser As New FileIO.TextFieldParser(stringStream)
            
            Do
               
                textline = parser.ReadLine()
                splitter = Split(textline)

                Dim itemname As String = ""

                If name = splitter(0) Then
                    For x = 1 To UBound(splitter)
                        If splitter(x) = "" Then
                        Else

                            itemname =  splitter(x)
                        End If


                    Next

                    itemname = itemname.Replace(Chr(34), "")

                    Item_Grid.Rows.Add()
                    Item_Grid.Rows(Item_Grid.RowCount - 1).Cells(0).Value = itemname
                    Item_Grid.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter


                    Exit Do

                End If
              
                count = count + 1

               
            Loop Until parser.EndOfData

        End Using



Was This Post Helpful? 0
  • +
  • -

#12 Transistor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 20-November 13

Re: Retrieve text from file with Split

Posted 24 April 2020 - 10:45 AM

Ok think I just figured out a solution, it seems to be working anyway.

I just added this splitter = textline.Split('"'); to here -

                    if (name == splitter[0])
                    {
                        splitter = textline.Split('"');

                         for (x = 1; x < splitter.GetUpperBound(0); x++)
                         {


Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7453
  • View blog
  • Posts: 25,095
  • Joined: 05-May 12

Re: Retrieve text from file with Split

Posted 24 April 2020 - 11:34 AM

Can you show us your original VB.NET code? That TextFieldParser class was originally written for VB.NET, but is usable in C#. If it was working correctly in VB.NET, then it should still work correctly in C#.

Apparently I didn't hit refresh often enough...
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7453
  • View blog
  • Posts: 25,095
  • Joined: 05-May 12

Re: Retrieve text from file with Split

Posted 24 April 2020 - 04:09 PM

Unless I'm missing something (and it wouldn't be the first time), why are you even using a TextFieldParser if there are no text fields? Right now all you are doing really doing with the TextFieldParser is calling ReadLine(). Since the StringReader is a TextReader, you could just have been calling ReadLine() on it.

So your code could be simplified to something like:
var fileContent = Properties.Resources.itemlist;
using (var reader = System.IO.StringReader(fileContent))
{
    string line = reader.ReadLine();
    while (line != null)
    {
        string spaceSplit = line.Split(' ');

        if (spaceSplit.Length >= 1 && spaceSplit[0] == name)
        {
            int startQuote = line.IndexOf('"');
            int endQuote = line.LastIndexOf('"');
            string value = "";
            if (startQuote + 1 < endQuote)
            {
                value = line.SubString(startQuote + 1, endQuote - startQuote - 1);
            }
            int newRow = item_Grid.Rows.Add();
            item_Grid.Rows[newRow].Cells[0].Value = value;
        }
 
        line = reader.ReadLine();
    }
}



The technique above of using IndexOf() is explained in the Microsoft documentation for the Split() method.
Was This Post Helpful? 0
  • +
  • -

#15 Transistor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 20-November 13

Re: Retrieve text from file with Split

Posted 24 April 2020 - 05:09 PM

When I first made this in VB, long ago, I went searching for how to do certain things and came across an example someone posted that was similar to what I was doing. So I applied it to what I was doing. It worked, got the job done, so I didn't question it. Good or bad...



Thank you for the examples!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1