user2546071
user2546071

Reputation: 51

SubString editing

I've tried a few different methods and none of them work correctly so I'm just looking for someone to straight out show me how to do it . I want my application to read in a file based on an OpenFileDialog.

When the file is read in I want to go through it and and run this function which uses Linq to insert the data into my DB.

 objSqlCommands.sqlCommandInsertorUpdate

However I want to go through the string , counting the number of ","'s found . when the number reaches four I want to only take the characters encountered until the next "," and do this until the end of the file .. can someone show me how to do this ?

Based on the answers given here my code now looks like this

string fileText = File.ReadAllText(ofd.FileName).Replace(Environment.NewLine, ",");

               int counter = 0;
               int idx = 0;
               List<string> foo = new List<string>();

               foreach (char c in fileText.ToArray())
               {
                   idx++;
                   if (c == ',')
                   {
                       counter++;
                   }
                   if (counter == 4)
                   {
                       string x = fileText.Substring(idx);
                       foo.Add(fileText.Substring(idx, x.IndexOf(',')));
                       counter = 0;
                   }
               }

               foreach (string s in foo)
               {
                   objSqlCommands.sqlCommandInsertorUpdate("INSERT", s);//laClient[0]);
               }

However I am getting an "length cannot be less than 0" error on the foo.add function call , any ideas ?

Upvotes: 0

Views: 222

Answers (4)

Enigmativity
Enigmativity

Reputation: 117144

It's not clear to me if you're after every fifth piece of text between the commas or if there are multiple lines and you want only the fifth piece of text on each line. So I've done both.

Every fifth piece of text:

var text = "1,2,3,4,i am some text,6,7,8,9"
    + ",10,11,12,13,14,15,16,17,18,19,20";

var everyFifth =
    text
        .Split(',')
        .Where((x, n) => n % 5 == 4);

everyFifth

Only the fifth piece of text on each line:

var lines = new []
{
    "1,2,3,4,i am some text,6,7",
    "8,9,10,11,12,13,14,15",
    "16,17,18,19,20",
};

var fifthOnEachLine =
    lines
        .Select(x => x.Split(',')[4]);

fifthOnEachLine

Upvotes: 0

Raidri
Raidri

Reputation: 17560

File.ReadAllText reads a text file to a string and Split turns that string into an array seperated at the commas:

File.ReadAllText(OpenDialog.FileName).Split(',')[4]

If you have more than one line use:

File.ReadAllLines(OpenDialog.FileName).Select(l => l.Split(',')[4])

This gives an IEnumerable<string> where each string contains the wanted part from one line of the file

Upvotes: 1

Tim
Tim

Reputation: 28530

As Raidri indicates in his answer, String.Split is definitely your friend. To catch every fifth word, you could try something like this (not tested):

string fileText = File.ReadAllText(OpenDialog.FileName).Replace(Environment.NewLine, ",");

string words[] = fileText.Split(',');

List<string> everFifthWord = new List<string>();

for (int i = 4; i <= words.Length - 1, i + 5)
{
    everyFifthWord.Add(words[i]);
}

The above code reads the selected file from the OpenFileDialog, then replaces every newline with a ",". Then it splits the string on ",", and starting with the fifth word takes every fifth word in the string and adds it to the list.

Upvotes: 1

DGibbs
DGibbs

Reputation: 14618

A Somewhat hacky example. You would pass this the entire text from your file as a single string.

string str = "1,2,3,4,i am some text,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20";
int counter = 0;
int idx = 0;
List<string> foo = new List<string>();

foreach (char c in str.ToArray())
{
     idx++;
     if (c == ',')
     {
          counter++;
     }
     if (counter == 4)
     {
          string x = str.Substring(idx);
          foo.Add(str.Substring(idx, x.IndexOf(',')));
          counter = 0;
     }
}

foreach(string s in foo)
{
     Console.WriteLine(s);
}
Console.Read();

Prints:

  • i am some text
  • 9
  • 13
  • 17

Upvotes: 1

Related Questions