Take what’s between the quotes

Asked

Viewed 194 times

1

As I do to get only what is inside double quotes, the problem is the following, it is reading a file txt q on each line has a file path, as this below

"Dinosfuls Mods.bmd" 45454545s 454545

How do I pick up only that part without quotation marks?

Dinosfuls Mods.bmd**

file. Name = Dinosfuls Mods.bmd

file. Hash = 45454545s

file. Size = 454545

public static void AddFile(string File)
{
    Globals.File file = new Globals.File();
    
    string texto = File.Split(' ')[0];

    int valorPos = texto.IndexOf(@"""") + 1;
    string valorEntreAspas = texto.Substring(valorPos, texto.IndexOf(@"""", valorPos) - valorPos);

    file.Name = valorEntreAspas;
    //file.Hash = File.Split(' ')[1];
    //file.Size = Convert.ToInt64(File.Split(' ')[2]);

    //Globals.Files.Add(file);
}
  • And what’s your problem?

  • is only picking up before the file space le only "Dinofuls

  • It’s a little hard to understand. One of the reasons that is wrong is that the problem is not well defined, programming is detail, when one is missing or is wrong the code goes wrong. The code does several things that only you know because. We don’t have enough data entry information and exact output.

  • i need this code to take this line from txt "Dinosfuls Mods.bmd" 454545s 454545, read only what’s inside the quotes and play within a variable......

  • this line "Dinosfuls Mods.bmd" 454545454545 is in the text variable.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

Show 1 more comment

3 answers

2

There’s some weird stuff in this code, I’m gonna try to post something that looks like it solves.

public static void AddFile(string texto) {
    int pos = texto.IndexOf("\"") + 1; //se achar nada causará erro
    var entreAspas = texto.Substring(pos, texto.IndexOf("\"", pos) - valorPos);
    texto = texto.Substring(texto.IndexOf("\"", valorPos) + 1);
    var palavras = texto.Split(' ');
    Globals.Files.Add(new Globals.File { Name = entreAspas, Hash = palavras[0], Size = Convert.ToInt64(palavras[1]));
}

I put in the Github for future reference.

  • did not work, I will try to explain denovo kkkk, like this code is responsible for reading each row of a txt file and each line of txt has 3 columns, I need to take only the first column remove the quotes and put in a variable...

1

Try the following code... just remove what is not necessary.

        //Simulando o recebimento da string
        string str = "\"Dinosfuls Mods.bmd\" 454545s 454545";
        //Separa as colunas e pega a segunda contando que sempre vai vir duas aspas na string
        string primeiraCol = str.Split('\"')[1];

0

Have you thought about using a regular expression?

    public static void AddFile(string file)
    {
        string pattern = @"([""'])(?:(?=(\\?))\2.)*?\1";

        Regex rx = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);

        var match = rx.Match(file);

        if (match.Success)
        {
            var fileName = match.Value.Replace("\"", string.Empty);
            var values  = file.Replace(match.Value, string.Empty).Split(' ');
            var hash = values[1];
            var size = values[2];
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.