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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17386978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:23:30  来源:igfitidea点击:

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

c#.net-4.0

提问by user2442470

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.

采纳答案by TGH

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

Use Substring

使用子串

回答by logixologist

Here is basically the way you would get it:

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

LEFT(textbox1.text, 3)

回答by Kuba Szostak

Text lower than 3 characters will throw an exception. So check text length first:

少于 3 个字符的文本将引发异常。所以首先检查文本长度:

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