2

我正在寻找这个,因为我看到 Microsoft Word 具有此功能。句子案例和切换案例。

我想在菜单项中添加这两个项目,这样如果我单击“转换为句子大小写”,选定的文本将转换为句子大小写,或者如果我单击“转换”则切换大小写。切换大小写'...

我知道大写,小写和正确的大小写,代码如下,但是对于句子大小写和切换大小写呢?

Private Sub LowerCaseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LowerCaseToolStripMenuItem.Click
    TextBox1.SelectedText = TextBox1.SelectedText.ToLower
End Sub
Private Sub UpperCaseToolStripMenuItem_Click_1(sender As Object, e As EventArgs) Handles UpperCaseToolStripMenuItem.Click
    TextBox1.SelectedText = TextBox1.SelectedText.ToUpper
End Sub
Private Sub TitleCaseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TitleCaseToolStripMenuItem.Click
    TextBox1.SelectedText = StrConv(TextBox1.SelectedText, VbStrConv.ProperCase)
End Sub

但是,是的,仅将代码用于 VB.Net,而不用于 C# 或其他。我只是去 vb.net,我得到了两到 3 篇关于 C# 的文章,但没有一篇关于 vb.net 的文章,所以请...

您也可以放置项目链接来帮助我谢谢

4

2 回答 2

0

我知道这是很长的路要走。但是,它有效。希望它有所帮助。

    Dim strInput, m, n As String
    Dim strOutput As String
    Dim strTemp As String
    Dim i, i1 As Integer

   strInput = "converted. to title case"
    n = Len(strInput)
    For i1 = 1 To n
        Try
            m = strInput(i1 - 1)
        Catch ex As Exception
            Exit For
        End Try
        If m = " " Then
            n = Len(strInput)
            strInput = Mid(strInput, i1 + 1, n)
            i = Len(strInput) - i1
            n = i1
            i1 = 0
            strTemp = strTemp & " "
        Else
            If i1 = 1 Then
                strTemp = strTemp & LCase(Mid(strInput, i1, 1))
            Else
                strTemp = strTemp & UCase(Mid(strInput, i1, 1))

            End If
        End If
    Next i1
    strOutput = strTemp 'for tOGGLE Case
Dim sentancecase As String = StrConv(title, VbStrConv.ProperCase)' For Sentence Case
于 2013-12-08T16:43:44.903 回答
0

我前段时间写了一个方法来做你需要的。

用法:

· 对于句子大小写(TitleCase)

TextBox1.SelectedText = 
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Title)

· ToggleCase

TextBox1.SelectedText = 
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Toggle)

· 正确大小写(WordCase)

TextBox1.SelectedText = 
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Word)

· 小写

TextBox1.SelectedText = 
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Lower)

· 大写

TextBox1.SelectedText = 
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Upper)

#Region " String Renamer "

' [ String Renamer ]
'
' // By Elektro H@cker
'
' Examples :
'
' MsgBox(StringRenamer.Rename("Hello World!", StringRenamer.StringCase.Upper))
' MsgBox(StringRenamer.Rename("Hello World!", StringRenamer.StringCase.Upper, New System.Text.RegularExpressions.Regex("\s+"), "-", RegexOptions.None))

Public Class StringRenamer

    Private Shared output As String = String.Empty

    Public Enum StringCase As Short

        ''' <summary>
        ''' LowerCase
        ''' 
        ''' [Example]
        ''' Input : ABCDEF
        ''' Output: abcdef
        ''' </summary>
        Lower = 0

        ''' <summary>
        ''' UpperCase.
        ''' 
        ''' [Example]
        ''' Input : abcdef
        ''' Output: ABCDEF
        ''' </summary>
        Upper = 1

        ''' <summary>
        ''' TitleCase.
        ''' 
        ''' [Example]
        ''' Input : abcdef
        ''' Output: Abcdef
        ''' </summary>
        Title = 2

        ''' <summary>
        ''' WordCase.
        ''' 
        ''' [Example]
        ''' Input : abc def
        ''' Output: Abc Def
        ''' </summary>
        Word = 3

        ''' <summary>
        ''' CamelCase (With first letter to LowerCase).
        ''' 
        ''' [Example]
        ''' Input : ABC DEF
        ''' Output: abcDef
        ''' </summary>
        Camel_Lower = 4

        ''' <summary>
        ''' CamelCase (With first letter to UpperCase).
        ''' 
        ''' [Example]
        ''' Input : ABC DEF
        ''' Output: AbcDef
        ''' </summary>
        Camel_Upper = 5

        ''' <summary>
        ''' MixedCase (With first letter to LowerCase).
        ''' 
        ''' [Example]
        ''' Input : ab cd ef
        ''' Output: aB Cd eF
        ''' </summary>
        Mixed_TitleLower = 6

        ''' <summary>
        ''' MixedCase (With first letter to UpperCase).
        ''' 
        ''' [Example]
        ''' Input : ab cd ef
        ''' Output: Ab cD Ef
        ''' </summary>
        Mixed_TitleUpper = 7

        ''' <summary>
        ''' MixedCase (With first letter of each word to LowerCase).
        ''' 
        ''' [Example]
        ''' Input : ab cd ef
        ''' Output: aB cD eF
        ''' </summary>
        Mixed_Word_Lower = 8

        ''' <summary>
        ''' MixedCase (With first letter of each word to UpperCase).
        ''' 
        ''' [Example]
        ''' Input : ab cd ef
        ''' Output: Ab Cd Ef
        ''' </summary>
        Mixed_Word_Upper = 9

        ''' <summary>
        ''' ToggleCase.
        ''' 
        ''' [Example]
        ''' Input : abc def ghi
        ''' Output: aBC dEF gHI
        ''' </summary>
        Toggle = 10

        ''' <summary>
        ''' Inverts the characters.
        ''' 
        ''' [Example]
        ''' Input : Hello World!
        ''' Output: hELLO wORLD!
        ''' </summary>
        Inverted = 11

    End Enum

    ''' <summary>
    ''' Rename a string to the specified StringCase.
    ''' </summary>
    Public Shared Function Rename(ByVal Text As String,
                                  ByVal sCase As StringCase) As String

        Select Case sCase

            Case StringCase.Lower

                Return Text.ToLower

            Case StringCase.Upper

                Return Text.ToUpper

            Case StringCase.Title

                Return Char.ToUpper(Text.First) & Text.Substring(1).ToLower

            Case StringCase.Word

                Return Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Text.ToLower)

            Case StringCase.Camel_Lower

                Return Char.ToLower(Text.First) &
                       Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Text.ToLower).
                       Replace(" ", "").
                       Substring(1)

            Case StringCase.Camel_Upper

                Return Char.ToUpper(Text.First) &
                       Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Text.ToLower).
                       Replace(" ", "").
                       Substring(1)

            Case StringCase.Mixed_TitleLower

                output = String.Empty

                For X As Integer = 0 To Text.Length - 1 Step 2

                    Try
                        output &= Char.ToLower(Text(X)) &
                                  Char.ToUpper(Text(X + 1))

                    Catch ex As IndexOutOfRangeException
                        output &= Char.ToLower(Text(X))

                    End Try

                Next X

                Return output

            Case StringCase.Mixed_TitleUpper

                output = String.Empty

                For X As Integer = 0 To Text.Length - 1 Step 2

                    Try
                        output &= Char.ToUpper(Text(X)) &
                                  Char.ToLower(Text(X + 1))

                    Catch ex As IndexOutOfRangeException
                        output &= Char.ToUpper(Text(X))

                    End Try

                Next X

                Return output

            Case StringCase.Mixed_Word_Lower

                output = String.Empty

                For Each token As String In Text.Split
                    output &= StringRenamer.Rename(token, StringCase.Mixed_TitleLower) & " "
                Next token

                Return output

            Case StringCase.Mixed_Word_Upper

                output = String.Empty

                For Each token As String In Text.Split
                    output &= StringRenamer.Rename(token, StringCase.Mixed_TitleUpper) & " "
                Next token

                Return output

            Case StringCase.Toggle

                output = String.Empty

                For Each token As String In Text.Split
                    output &= Char.ToLower(token.First) & token.Substring(1).ToUpper & " "
                Next token

                Return output

            Case StringCase.Inverted

                output = String.Empty

                For Each c As Char In Text

                    output &= If(Char.IsLower(c),
                                 Char.ToUpper(c),
                                 Char.ToLower(c))

                Next c

                Return output

            Case Else

                Return Nothing

        End Select

    End Function

    ''' <summary>
    ''' Rename a string to the specified StringCase,
    ''' Also find and replace text after rename.
    ''' </summary>
    Public Shared Function Rename(ByVal Text As String,
                                  ByVal sCase As StringCase,
                                  ByVal FindWhat As System.Text.RegularExpressions.Regex,
                                  ByVal ReplaceWith As String,
                                  ByVal RegExIgnoreCase As System.Text.RegularExpressions.RegexOptions) As String

        Return System.Text.RegularExpressions.
               Regex.Replace(StringRenamer.Rename(Text, sCase), FindWhat.ToString, ReplaceWith, RegExIgnoreCase)

    End Function

End Class

#End Region
于 2013-12-08T16:31:27.143 回答