> For the complete documentation index, see [llms.txt](https://apwt.gitbook.io/cursus-pro-oo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://apwt.gitbook.io/cursus-pro-oo/semester-2-oop/h11-objecten-al-dan-niet-aanmaken/strings.md).

# Spelen met strings

Je kan objecten aanmaken door een omschrijving ervan in tekst om te zetten naar een constructoroproep. We noemen dit proces "deserialisatie". Een object omzetten naar een omschrijving noemen we "serialisatie". Deze omschrijving kan verschillende conventies volgen: XML, JSON, YAML, CSV,...

{% hint style="info" %}
We hebben [eerder in de cursus ](/cursus-pro-oo/semester-1-programming-principles/h10-gevorderde-tekstverwerking/input-en-output-van-tekstbestanden.md)geleerd hoe we CSV-bestanden moesten uitlezen. Fris dit zo nodig op, want we gaan nu een stap verder.
{% endhint %}

### Objecten deserializeren met CSV

CSV wordt vaak gebruikt om objecten tussen programma's te verplaatsen. Een object inlezen vanaf een CSV bestand is een vorm van deserializeren.

```csharp
// Speler voornaam, familienaam, geboortejaar
string[] lijnen = File.ReadAllLines(@"C:\spelers.csv");
Speler[] spelers = new Speler[lijnen.Length];
for (int i = 0; i < lijnen.Length; i++)
{
    string[] kolomwaarden = lijnen[i].Split(',');
    spelers[i] = new Speler(kolomwaarden[0],kolomwaarden[1],Convert.ToInt32(kolomwaarden[2]));
}
```

### CSV wegschrijven

Je kan tekst uit een bestand lezen, maar uiteraard kan je ook naar een bestand wegschrijven. De 2 eenvoudigste manieren zijn:

* `File.WriteAllText`: deze gebruik je als je 1 enkele string wilt wegschrijven
* `File.WriteAllLines`: deze is de omgekeerde van `ReadAllLines()` en zal een array van strings wegschrijven.

Een voorbeeld:

```csharp
string[] stringArray = new string[]
    {
        "cat",
        "dog",
        "arrow"
    };


File.WriteAllLines("file.txt", stringArray);
```

**Opgelet met het schrijven naar bestanden: dit zal onherroepelijk het target bestand overschrijven.** Gebruik `if(File.Exists(pathtofile))` om te controleren of een bestand bestaat of niet. Eventueel kan je dan aan de gebruiker bevestiging vragen of je deze effectief wilt overschrijven.

Wil je CSV-bestand maken dan zal je eerst met `String.Join` of met stringinterpolatie een komma-separated lijst maken, bijvoorbeeld:

```csharp
Speler[] spelers = {
    new Speler("Tim","Dams",1981),
    new Speler("Jos","Stoffels",1970),
    new Speler("Mounir","Hamdaoui",1984)
};

string[] lines = new string[spelers.Length];
for (int i = 0; i < lines.Length; i++)
{
    Speler speler = spelers[i];
    lines[i] = $"{speler.Voornaam},{speler.Familienaam},{speler.Geboortejaar}";
}

System.IO.File.WriteAllLines("spelers.csv", lines);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://apwt.gitbook.io/cursus-pro-oo/semester-2-oop/h11-objecten-al-dan-niet-aanmaken/strings.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
