【问题标题】:How to capitalize letter only in beginning of sentences, and the next word is normal如何只在句首大写字母,下一个单词正常
【发布时间】:2021-01-29 12:31:45
【问题描述】:

我用这个:

Static PreviousLetter As Char
    If PreviousLetter = " "c Or TextBox1.Text.Length = 0 Then
        e.KeyChar = Char.ToUpper(e.KeyChar)
    End If
    PreviousLetter = e.KeyChar

但结果总是:

Good Night Every Body

我怎样才能只大写句子中的第一个字母,而让其他单词正常?我想要的结果是:

Good night every body

【问题讨论】:

    标签: vb.net capitalize sentence


    【解决方案1】:

    不要使用静态变量来保存前一个字符。一般来说,这是不需要的,也是不好的做法。

    然后,虽然您的问题有点不清楚,但假设您希望对 TextBox1 的文本进行更改,您可能希望在更改后将文本设置回 TextBox。

    所以解决方案可能如下所示:

     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 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
    

    更新

    基于 cmets,如果您想即时进行此更改(即当用户在 TextBox 中键入时),那么您还需要操作光标。本质上,您需要在更改文本之前存储光标位置,然后在更改后恢复位置。

    另外,我将在KeyUp 事件中执行这些更改,而不是在KeyPress 事件中。 KeyUp 发生在 TextBox 注册更改以响应按键按下之后。

     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
    

    【讨论】:

    • 感谢您的回答,但是当我尝试它时,这个词是不对的,如果我尝试写一个“笔记本”,这个词就是“koobeton”。我把代码放在 textbox1 keypress 中,
    • 如果您想即时执行更改(即当用户键入文本时),您还需要操作光标。我会在一秒钟内更新答案..
    【解决方案2】:

    TextInfo.ToTitleCase 方法将指定的字符串转换为标题大小写。

    txtName.Text = Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(txtName.Text.ToLower)
    

    您必须先将整个文本转换为小写,否则它将无法按预期工作:

    (除了完全大写的单词,它们被认为是 简称)

    【讨论】:

    • 你能附上一个链接吗?我在 NetFramework 文档中找不到它。
    • 谢谢。我读到这是不好的做法。在我的情况下使用 CSS 可能更好。谢谢
    【解决方案3】:

    你绝对可以的。

    RegularExpressions 是最直接的方式...类似于你正在做的事情,你可以将它们组合起来。

    很多人已经看过这个了。这是一个:
    Formatting sentences in a string using C#

    使用该正则表达式字符串替换而不是您的 Char.ToUpper。
    或者只是等到所有文本都输入后再运行一次,例如当控件失去焦点时。

    【讨论】:

      【解决方案4】:
      ' 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"
      

      【讨论】:

        【解决方案5】:

        试试这个

            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
        

        【讨论】:

          【解决方案6】:

          创建一个扩展:

          Public Function ToUppercaseFirstLetter(ByVal Text As String) As String
              If Text = "" Then Return Text
              Dim array() As Char = Text.ToCharArray
              array(0) = Char.ToUpper(array(0))
              Return array
          End Function
          

          【讨论】:

            【解决方案7】:

            试试这个代码

                If Char.IsLetter(e.KeyChar) Then
            
                    If txtType.Text.Length = 0 Then
                        e.KeyChar = Char.ToUpper(e.KeyChar)
                    Else
                        e.KeyChar = Char.ToLower(e.KeyChar)
                    End If
            
                End If
            

            【讨论】:

              猜你喜欢
              • 2020-08-30
              • 2014-07-11
              • 2020-07-16
              • 1970-01-01
              • 2016-12-05
              • 2015-11-10
              • 1970-01-01
              • 1970-01-01
              • 2023-04-04
              相关资源
              最近更新 更多