mykds
mykds

Reputation: 63

Read string word by word with .Substring?

I'v got the following String string gen = "Action;Adventure;Drama;Horror; I tried to seperate the string word by word with .substring like: gen.Substring(gen.IndexOf(';')+1, gen.IndexOf(';')) But my output is just "Advent".

Any help?

Background: The string collects the names of checkboxes that are checked. The string is then saved in a database. I want to read out the string an check each checkbox on another form.

Upvotes: 3

Views: 152

Answers (2)

Umair M
Umair M

Reputation: 10740

Split it like this:

public class Example
{
   public static void Main()
   {
      String value = "Action;Adventure;Drama;Horror";
      Char delimiter = ';';
      String[] substrings = value.Split(delimiter);
      foreach (var substring in substrings)
         Console.WriteLine(substring);
   }
}

Upvotes: 4

brunnerh
brunnerh

Reputation: 185047

Just split it:

var parts = gen.Split(';');

(Then you can iterate over that via foreach.)

Upvotes: 9

Related Questions