从文本框中提取部分文本

本文关键字:文本 提取部 | 更新日期: 2023-09-27 18:24:08

我在文本框中有此文本:mytextvariable.mytext

如何只提取点之前的文本?

例如:textbox1=mytext.text

按钮点击

textbox2=不带.text 的mytext

从文本框中提取部分文本

只需使用简单的字符串操作:

string text = textBox1.Text;
int firstDot = text.IndexOf('.');
if (firstDot != -1)
{
    string firstPart = text.Susbtring(0, firstDot);
    textBox2.Text = firstPart;
}
else
{
    // Handle case with no dot
}

从根本上说,这与来自文本框的文本无关——实际上,它只是一个字符串。

var beforeDot = new String(textBox.Text.TakeWhile(c => c != '.').ToArray());
if (mytextvariable.mytext.Contains("."))
  String stuffBeforeTheDot = mytextvariable.mytext.Substring(0, mytextvariable.mytext.IndexOf("."));

您可以为此使用"拆分"。

myText.Split('.').First();