I have a Textbox
that is fetching the text from another Form, and now I want to copy a value that is in this Textbox
.
Attached image with what I want:
I have a Textbox
that is fetching the text from another Form, and now I want to copy a value that is in this Textbox
.
Attached image with what I want:
Another alternative is the String.Substring
, used to extract part of a string .
To extract only the filename, without the extension, use the GetFileNameWithoutExtension
of namespace System.IO
.
In the event Click
of the Generate button put 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;
Result:
I think the easiest way is to use String.Split from C #, by dividing the string through the 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 words array, in which case get the index 1 from the array, which should contain the name you are looking for.
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);
}
}
If you need to find more than one filename, simply change to use the Matches
method of the Regex
class. It returns a collection of values found in the text. Then it is enough to iterate and to 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);
}