Transform a string into several substrings whose contents are in apostrophes

5

I am creating a program in C # that reads a text file and one of the lines of this file has the following format:

'Column 1' 'Column 2' 'Column 3'

I want to turn this line into an array of strings so that the answer looks like this:

    Colunas[0] = "Coluna 1"
    Colunas[1] = "Coluna 2"
    Colunas[2] = "Coluna 3"

That is, I want it to identify each string within apostrophes and store them in the array. I tried doing this by reading the whole line using the following code:

    string Linha = Leitor.ReadLine(); //Leitor é o StreamReader que lê o arquivo

And then I tried to use the method linha.Split

    var NomesColunas = linha.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

But then the result was as follows: {"Column", "1", "Column", "2", "Column", "3"}. I tried to use the apostrophe as char to do the Split but it is giving compilation error, I am not hitting the syntax.

    
asked by anonymous 25.11.2016 / 18:44

3 answers

3

Using regex would look something like:

'([^']+)'

This way it will pick up "unlimited"

An example would be this (I do not have much understanding of C #, any failure can criticize / correct):

using System;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        string linha = "'Oi 1' 'tchau 2' 'hello 3' 'good bye 4'";
        string regex = @"'([^']+)'";
        MatchCollection match = Regex.Matches(linha, @regex);

        string[] dados = new string[match.Count];

        for (int i = 0; i < dados.Length; i++)
        {
            dados[i] = match[i].Groups[1].Value;
            Console.WriteLine(dados[i]);
        }
    }
}

Note that I used string[] instead of List , because I understood that this was how I was working, but this does not change the understanding at all, just adjust.

25.11.2016 / 19:13
2
Obviously, if you have an apostrophe to use as char you would have to escape it so as not to confuse the compiler and mix what is a character delimiter and the character ( '\'' ). But since it has more than one character by default then you have to use string and not char .

You have to remove the initial and final apostrophe and then separate ( Split() ) by the standard that is "an apostrophe, a space, an apostrophe". So:

using static System.Console;
using System;

public class Program {
    public static void Main() {
        var texto = "'Coluna 1' 'Coluna 2' 'Coluna 3'";
        texto = texto.Substring(1, texto.Length - 2);
        var items = texto.Split(new string[] {"' '"}, StringSplitOptions.None);
        foreach (var item in items) {
            WriteLine(item);
        }
    }
}

See running on dotNetFiddle and on CodigGround .

This should be the simplest form. You can develop a more elegant algorithm, but it will not be that simple.

    
25.11.2016 / 18:56
1

The answer above has what you need, I've implemented a slightly different form here.

using System;

public class Program
{
    public static void Main()
    {
        string linha = "'Coluna 1' 'Coluna 2' 'Coluna 3'";

        linha = linha.Replace("' ",",").Replace("'","");

        string[] linhas = linha.Split(',');
        foreach(var item in linhas)
        {
            Console.WriteLine(item);
        }
    }
}

link

    
25.11.2016 / 19:04