如何在vb.net中将所选文本替换为句子

时间:2013-12-08 13:01:20

标签: vb.net visual-studio-2008 case sentencecase

我正在寻找这个,因为我看到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,我在C#上得到2到3篇文章,但对vb.net没有,所以请...

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

2 个答案:

答案 0 :(得分:0)

我在很久以前写过一种方法来做你需要的。

用法:

·对于句子案例(TitleCase)

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

·ToggleCase

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

·对于ProperCase(WordCase)

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

·对于LowerCase

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

·对于UpperCase

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

答案 1 :(得分: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