简体   繁体   English

如何获取文本框文本的前 3 个字符?

[英]How to get first 3 characters of a textbox's text?

How do I get the first 3 characters of the text in a textbox?如何获取文本框中文本的前 3 个字符?

For example, textBox1.Text = "HITHEREGUYS"例如, textBox1.Text = "HITHEREGUYS"

When I get the first 3 characters, it should show HIT .当我得到前 3 个字符时,它应该显示HIT

string result = textBox1.Text.Substring(0,3);

使用子串

Text lower than 3 characters will throw an exception.少于 3 个字符的文本将引发异常。 So check text length first:所以首先检查文本长度:

string result = textBox1.Text.Length <= 3 ? textBox1.Text : textBox1.Text.Substring(0, 3);

这里基本上是你得到它的方式:

LEFT(textbox1.text, 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Windows控件的文本框中获取文本? 我怎么知道这是一个文本框? - How to get text from a windows control's textbox? and how do I know it's a textbox? 如何从ListViewItem的DataTemplate中的TextBox获取文本 - How to get text from TextBox inside ListViewItem's DataTemplate 如何自定义Winforms文本框文本字符分隔? - How to customize Winforms textbox text characters separated? 如何从长度未知的文本框中的文本中获取每两个字符的 substring - How can I get the substring of every two characters from text in a textbox with an unknown length 如何将WinForms TextBox的前几个字符设置为只读? - How to set the first few characters of a WinForms TextBox to Read-Only? 如何在Textbox的TextChanged事件中获取旧文本和更改文本框的文本? - How to get old text and changed text of textbox on TextChanged event of textbox? 如何使文本框的ControlTemplate中的按钮更新文本框的文本? - How to make a button in a Textbox's ControlTemplate update the text of the TextBox? 如何在另一个窗口中读取textBox的文本 - How to read a textBox's Text in another window 如何将文本框的文本与文件的内容绑定? - How to bind the Text of a TextBox with a File's content? 如何查找WPF TextBox的更改文本? - How to find WPF TextBox's changed text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM