Copy value between quotation marks C#

Asked

Viewed 1,452 times

3

I have a Textbox that is fetching the text from another form, and now I intend to copy a value that is in that Textbox.

Attached image with what I want:

inserir a descrição da imagem aqui

  • was quite different from the request initially.. Next time try to get more attention :P

  • You’re having trouble implementing what you answered in the answers?

3 answers

2


Another alternative is the method String.Substring, used to extract part of a string.

To extract only the file name, without the extension, use the function GetFileNameWithoutExtension of namespace System.IO.

At the event Click button Beget enter the code:

string texto = textBox1.Text;
int valorPos = texto.IndexOf(@"""") + 1;
string valorEntreAspas = texto.Substring(valorPos, texto.IndexOf(@"""", valorPos) - valorPos);
string arquivoSemExtensao = Path.GetFileNameWithoutExtension(valorEntreAspas);
label1.Text = arquivoSemExtensao;

See demonstração

Upshot:

inserir a descrição da imagem aqui

  • 1

    Perfect friend, working! Thank you very much!

1

I think the easiest way is to use the String.Split of C#, dividing the string by quotation marks.

string[] words = text.Split('"');
string[] parts = words[1].Split('.');
string result = parts[0];

Split splits a string into an array of strings using the " character as a separator. Then just go to the array words, and in that case, fetch the index 1 of the array, which should contain the name you are looking for.

  • Amigo @Blunt might be more specific?

  • I made an edit on the question Blunt.

  • @Godfathersantana the Split is enough to do this. First you split the original text using " as the delimiter, and then you get the string you want from the resulting array, split it using . as delimiter, and there is an array with 2 elements, of which the important thing is the index 0.

  • But @Blunt, where do I put this code? On Label? On Load? I’m in doubt... I don’t even know what code to put, I’m how to say it and well "getting caught up in the air".

  • That depends on the code. if you have no idea what you are doing is complicated...but where you need to place the code depends on how the application is mounted, and how the text is being entered in the textbox

  • Imagine that when I click on a button, what code I write for the label to show what’s in quotes.

  • @Godfathersantana if the textbox is called txt you have to do string[] Parts = txt. Split('"'), then use Parts[1] to make Split('.') and after this variable that results, use the Indice 0, which returns the string before the point. see my edited reply.

Show 3 more comments

1

You can use Regular Expressions to find the file name in the text:

using System;
using System.IO;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
    string texto = @"Isto é um exemplo. E quero que na label1 apareceça o que está dentro de aspas.
      Ou seja, o texto que se encontra ""aqui.txt"". 
      Porém a label1 não sabe o que estará na textbox.";

      var match = Regex.Match(texto, @"\""([\w\-. ]+)(\.)([\w\-. ]+)\""");
      string arquivoSemExtensao = Path.GetFileNameWithoutExtension(match.Value.Replace("\"", String.Empty));

      Console.WriteLine(arquivoSemExtensao);     
    }      
}

See working here

If you need to find more than one filename, simply change to use the method Matches class Regex. It returns a collection of values found in the text. Then simply iterate and remove the extension:

string texto = @"Isto é um exemplo. E quero que na label1 apareceça o que está dentro de aspas.
    Ou seja, o texto que se encontra ""aqui.txt"" ""teste.exe""  ""outro_arquivo.bat""
    Porém a label1 não sabe o que estará na textbox.";

var matches = Regex.Matches(texto, @"\""([\w\-. ]+)(\.)([\w\-. ]+)\""");
foreach (Match match in matches)
{
    string arquivoSemExtensao = Path.GetFileNameWithoutExtension(match.Value.Replace("\"", String.Empty));
    Console.WriteLine(arquivoSemExtensao);
}

Example here

  • User does not know what will be written in Textbox amigo @Marcusvinicius.

  • Yes, so I suggested using Regex. Any text that enters the textbox, if it has the pattern of a filename inside quotation marks, will be captured by the expression. I just set the text to illustrate.

  • So what do I put in string text = @".......";?

  • The value of the textbox: nomeDoSeuTextBox.Text

Browser other questions tagged

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