vb.net 如何只在句首大写字母,下一个单词正常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20860233/
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
How to capitalize letter only in beginning of sentences, and the next word is normal
提问by walk.this_way
I use this:
我用这个:
Static PreviousLetter As Char
If PreviousLetter = " "c Or TextBox1.Text.Length = 0 Then
e.KeyChar = Char.ToUpper(e.KeyChar)
End If
PreviousLetter = e.KeyChar
But the result is always:
但结果总是:
Good Night Every Body
How can I capitalize just the first letter in the sentence, leaving the other words normal? The result I want is:
如何只将句子中的第一个字母大写,而其他单词则保持正常?我想要的结果是:
Good night every body
回答by Mike Dinescu
Don't use a static variable to hold the previous char. It's not needed and bad practice in general.
不要使用静态变量来保存前一个字符。一般来说,这是不需要的,也是不好的做法。
Then, although it's a bit unclear from your question, assuming you wish perform the change on the text of TextBox1
, you will probably want to set the text back to the TextBox after changing it.
然后,虽然您的问题有点不清楚,但假设您希望对 的文本执行更改TextBox1
,您可能希望在更改后将文本设置回 TextBox。
So a solution might looks like this:
因此,解决方案可能如下所示:
If TextBox1.TextLength > 1 Then
TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1)
ElseIf TextBox1.TextLength = 1 Then
TextBox1.Text = TextBox1.Text.ToUpper()
EndIf
If you want to capitalize the first letter and force lowercase the rest you can modify the code above like so:
如果你想大写第一个字母并强制小写其余的你可以像这样修改上面的代码:
If TextBox1.TextLength > 1 Then
TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1).ToLower()
ElseIf TextBox1.TextLength = 1 Then
TextBox1.Text = TextBox1.Text.ToUpper()
EndIf
UPDATE
更新
Based on the comments, if you want to make this change on the fly (ie. as the user is typing in the TextBox) then you will also need to manipulate the cursor. Essentially you need to store the cursor position prior to changing the text, and then restore the position after the change.
根据评论,如果您想即时进行此更改(即,当用户在 TextBox 中键入时),那么您还需要操作光标。本质上,您需要在更改文本之前存储光标位置,然后在更改后恢复位置。
Also, I would perform these changes in the KeyUp
event as opposed to the KeyPress
event. The KeyUp
happens after the TextBox has registered the change in response to the key press.
此外,我会在KeyUp
事件中执行这些更改而不是KeyPress
事件。该KeyUp
文本框已注册响应按键变更后发生。
Dim startPos as Integer
Dim selectionLength as Integer
' store the cursor position and selection length prior to changing the text
startPos = TextBox1.SelectionStart
selectionLength = TextBox1.SelectionLength
' make the necessary changes
If TextBox1.TextLength > 1 Then
TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1).ToLower()
ElseIf TextBox1.TextLength = 1 Then
TextBox1.Text = TextBox1.Text.ToUpper()
EndIf
' restore the cursor position and text selection
TextBox1.SelectionStart = startPos
TextBox1.SelectionLength = selectionLength
回答by Top Systems
TextInfo.ToTitleCase
Method Converts the specified string to title case.
TextInfo.ToTitleCase
方法 将指定的字符串转换为标题大小写。
txtName.Text = Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(txtName.Text.ToLower)
You have to convert the whole text to lowercase first or else it won't work as expected:
您必须先将整个文本转换为小写,否则它将无法按预期工作:
(except for words that are entirely in uppercase, which are considered to be acronyms)
(完全大写的单词除外,它们被认为是首字母缩写词)
回答by UDon
' With VB.net you could use the Mid() function also for assignments
Dim s as string = "good evening"
If Len(s)>0 Then Mid(s, 1, 1) = UCase(Left(s, 1))
'Result: "Good evening"
回答by Jadam
Try this
尝试这个
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
回答by Mike M
You definitely can do it.
你绝对可以做到。
RegularExpressions are the most direct way...similar to what you're doing, and you can combine them.
正则表达式是最直接的方式……类似于你正在做的,你可以将它们结合起来。
Plenty of people have already looked at this. Here's one:
Formatting sentences in a string using C#
很多人已经看过这个了。这是一个:
使用 C# 格式化字符串中的句子
Use that regular expression string replacement instead of your Char.ToUpper.
Or just wait until all the text is entered and run it once, such as when the control loses focus.
使用该正则表达式字符串替换而不是您的 Char.ToUpper。
或者只是等到所有的文本都输入并运行一次,例如当控件失去焦点时。