如何在C#.NET中将字符串拆分为两个单词? [英] How to split a string into two words in C#.NET?
本文介绍了如何在C#.NET中将字符串拆分为两个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在textbox1中输入文本作为输入。
将文本分成两部分。
在textbox2中存储第一个单词,在textbox3中存储第二个单词。 />
i尝试了这段代码。它将第二个单词存储在textBox2和textBox3中。
请帮帮我。
我的尝试:
I want to take a text as input in textbox1.
break the text into two parts.
Store 1st word in textbox2 and 2nd word in textbox3.
i tried this code. it stores the 2nd word in textBox2 and textBox3.
Please help me.
What I have tried:
private void button1_Click(object sender, EventArgs e)
{
var input=textBox1.Text;
var matches = Regex.Matches(input, @"\w+[^\s]*\w+|\w");
foreach (Match match in matches)
{
var word = match.Value;
textBox2.Text = word;
}
foreach (Match match in matches)
{
var word = match.Value;
textBox3.Text = word;
}
}
推荐答案
试试这个,
it仅当他们输入两个单词时才有效。
Try this,
it works only if they enter two words.
var input = textBox1.Text;
var matches = Regex.Matches(input, @"\w+[^\s]*\w+|\w");
if (matches.Count == 2)
{
textBox2.Text = matches[0].Value;
textBox3.Text = matches[1].Value;
}
得到了解决方案。它工作正常。谢谢你..
got a solution. it works fine. thank u..
private void button1_Click(object sender, EventArgs e)
{
string s = textBox1.Text;
string[] words = s.Split(' ');
textBox2.Text = words[0];
textBox3.Text = words[1];
}
这篇关于如何在C#.NET中将字符串拆分为两个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文