Text cASES in Vb.NET

  1. Sentence Case (This is vb.net code)
    Public Function SentenceCase(ByVal stringToConvert As String) As String
            Dim newStringBuilder As New StringBuilder
            newStringBuilder.Append(stringToConvert.ToLower)
            newStringBuilder.Chars(0) = newStringBuilder.Chars(0).ToString.ToUpper
            For letterCount As Integer = 0 To newStringBuilder.Length - 1
                If newStringBuilder.Chars(letterCount) = "." Or newStringBuilder.Chars(letterCount) = "?" Or newStringBuilder.Chars(letterCount) = "!" Then
                    If letterCount < newStringBuilder.Length - 1 Then
                        If newStringBuilder.Chars(letterCount + 1) = " " Then
                            newStringBuilder.Chars(letterCount + 2) = newStringBuilder.Chars(letterCount + 2).ToString.ToUpper
                        Else
                            newStringBuilder.Chars(letterCount + 1) = newStringBuilder.Chars(letterCount + 1).ToString.ToUpper
                        End If
                    End If
                End If
            Next
            Return newStringBuilder.ToString
        End Function

    How To Use

    Msgbox (SentenceCase("This Is Vb.Net Code"))
  2. Toggle Case (tHIS iS vB.nET cODE)
    Public Shared Function Togglecase(ByVal origText As String) As String
            Dim counter As Integer
            Dim textParts() As String = Split(origText, " ")
    
            For counter = 0 To textParts.Length - 1
                If (textParts(counter).Length > 0) Then _
                   textParts(counter) = _
                   LCase(Microsoft.VisualBasic.Left( _
                   textParts(counter), 1)) & _
                   UCase(Mid(textParts(counter), 2))
            Next counter
    
            Return Join(textParts, " ")
        End Function

    How To Use

    Msgbox (Togglecase("This Is Vb.Net Code"))
  3. Capitalize Case (This Is Vb.Net Code)
    Public Shared Function CapitalizeCase(ByVal origText As String) As String
            Dim counter As Integer
            Dim textParts() As String = Split(origText, " ")
    
            For counter = 0 To textParts.Length - 1
                If (textParts(counter).Length > 0) Then _
                   textParts(counter) = _
                   UCase(Microsoft.VisualBasic.Left( _
                   textParts(counter), 1)) & _
                   LCase(Mid(textParts(counter), 2))
            Next counter
    
            Return Join(textParts, " ")
        End Function

    How To Use

    Msgbox (Togglecase("this is vb.net code"))
  4. Lower Case (this is vb.net code)

    How To Use

    MsgBox("THIS IS VB.NET CODE".ToLower)
  5. Upper Case (THIS IS VB.NET CODE)

    How To Use

    MsgBox("this is vb.net code".ToUpper)

Dani AI

Generated

The snippets posted by cover the common needs (sentence, word-capitalization, upper/lower). correctly noted there was no explicit question; the notes below aim to make those routines safer and clearer for long‑term use.

Common pitfalls in the posted code: no checks for Nothing/empty strings (IndexOutOfRange risk), comparing Char vs String (use char literals like '.'c), fragile index arithmetic around punctuation (punctuation at the end or extra spaces can break it), and a naming mismatch — the posted "Togglecase" actually lowercases the first letter and uppercases the rest of each word rather than toggling every character's case. Splitting on a single space loses tabs/multiple spaces/punctuation.

Safer, idiomatic alternatives (VB.NET):

Public Function ToTitleCaseSafe(input As String) As String
    If String.IsNullOrEmpty(input) Then Return input
    Dim ti = System.Globalization.CultureInfo.CurrentCulture.TextInfo
    Return ti.ToTitleCase(input.ToLower(System.Globalization.CultureInfo.CurrentCulture))
End Function
Public Function FlipCase(s As String) As String
    If s Is Nothing Then Return Nothing
    Dim sb As New System.Text.StringBuilder(s.Length)
    For Each ch As Char In s
        If Char.IsLetter(ch) Then
            sb.Append(If(Char.IsUpper(ch), Char.ToLower(ch), Char.ToUpper(ch)))
        Else
            sb.Append(ch)
        End If
    Next
    Return sb.ToString()
End Function
Public Function SentenceCaseSafe(s As String) As String
    If String.IsNullOrWhiteSpace(s) Then Return s
    Dim culture = System.Globalization.CultureInfo.CurrentCulture
    Dim sb As New System.Text.StringBuilder(s.ToLower(culture))
    Dim capitalizeNext As Boolean = True
    For i As Integer = 0 To sb.Length - 1
        Dim ch As Char = sb(i)
        If Char.IsLetter(ch) AndAlso capitalizeNext Then
            sb(i) = Char.ToUpper(ch)
            capitalizeNext = False
        ElseIf ch = "."c OrElse ch = "!"c OrElse ch = "?"c Then
            capitalizeNext = True
        ElseIf Not Char.IsWhiteSpace(ch) Then
            capitalizeNext = False
        End If
    Next
    Return sb.ToString()
End Function

Notes and cautions: prefer culture-aware APIs (TextInfo or StrConv for proper case), use char literals ('.'c), guard against Nothing/empty, test with hyphens, apostrophes, abbreviations (e.g., "e.g."), and test under different cultures (Turkish "i" is a classic gotcha). 's code is a solid start — the above adjustments improve safety and clarity for production use.

Recommended Answers

All 3 Replies

Not even 1 replay

Reply to what? All you did was post up some code without a question...

commented: Absolutley no reason for this down vote. Some doom puff wanting to make them selves feel better. +8

i meant a thanks replay
to encourage me
but never mind

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.