-1

I have the following problem. After the splitting I would like to get the author, title and booktype into variables like this.

string author = George Orwell
string title = Animal Farm
string booktype = novel

It's easy to print them out with a foreach loop but how do I get the values? Hope anyone can help me.

static void Main(string[] args)
{
    string[] book = new string[12];
    book[0] = "George Orwell###Animal Farm###Novel###";
    string value = book[0];
    string[] item = Regex.Split(value, "###");

    foreach (string newItem in item)
    {
        Console.WriteLine(newItem);
    }
    // prints out just fine

    // George Orwell
    // Animal Farm
    // Novel
    Console.ReadKey();
}
HimBromBeere
  • 32,045
  • 4
  • 46
  • 84
Mikael.N
  • 9
  • 2
  • 2
    `item[0]` should give you the name, `item[1]` should give you the title and so on. – mm8 Jul 17 '18 at 14:32
  • 5
    Your code shows that you know the result of `Split()` is an array, that you know how to access individual array elements, and that you know how to assign to variables. Put it all together. – TypeIA Jul 17 '18 at 14:32
  • is the order always author, title and booktype? – Fuzzybear Jul 17 '18 at 14:33
  • If you absolutely want to use `foreach` here is a topic for getting the index with it: https://stackoverflow.com/q/43021/4636715 – vahdet Jul 17 '18 at 14:34
  • Do you really mean that you want to convert those string values into things like "BookType enum" and "Author entity"? Everyone here's assuming you meant just reading the string values... – Dan Rayson Jul 17 '18 at 14:34
  • Could use `String.Split(book[0], "###")` instead of creating a new string just to store the same value –  Jul 17 '18 at 14:45
  • 1
    @MattBeldon note that `Split` is an *instance method* on a string not a static. So `book[0].Split(new[]{"###"}, StringSplitOptions.None);` – Jamiec Jul 17 '18 at 14:49

3 Answers3

7

Having split the string:

string[] item = Regex.Split(value, "###");

You have an array. You know that the first element is the name, the second is the title and the third is the type

string author = item[0];
string title = item[1];
string booktype = item[2];

You should probably do some validation before you try to read them, but in essence that's it.

Jamiec
  • 118,012
  • 12
  • 125
  • 175
0

Your question is "How do I get the values?"

The answer is, you already have! Console.WriteLine(newItem);

Maybe your question isn't exactly what you meant to ask ;)

Dan Rayson
  • 1,124
  • 1
  • 13
  • 31
-1
        //Initialise start of array
        var index = 0;
        //Check if array is large enough before accessing the index
        if (item.Length >= index)
        {
            author = item[index];
            index++;
        }
        if (item.Length >= index)
        {
            title = item[index];
            index++;
        }
        //No need to increment index here as no other checks are made
        if (item.Length >= index) booktype = item[index];
WKara
  • 109
  • 9