Results 1 to 9 of 9

Thread: [RESOLVED] How to detect two uppercase letters.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    Resolved [RESOLVED] How to detect two uppercase letters.

    Hey. I wanna do something so when someone is typing in a textbox it will detect two capital letters and change it to one, like for example, "I bought a NEw phone yesterday." It would detect that the word, "new" had two capital letters and it changes it back to "New."
    Anyone know how to do this?

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: How to detect two uppercase letters.

    Will StrConv() suffice for your needs?

    MsgBox ("I bought a NEw phone yesterday.", vbProperCase)

    EDIT: Ooops... forgot that it capitalizes start of all words. You'll have to scan string two characters at a time from position 1 to Len(string) - 1. Personally I would go for byte array implementation, but you can also use Mid().
    Last edited by leinad31; Apr 17th, 2008 at 10:48 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    Re: How to detect two uppercase letters.

    Ok. Thanks. I'll try find something like that

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: How to detect two uppercase letters.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim pos      As Long
    Dim strTemp  As String
    Dim lngCharA As Long
    Dim lngCharB As Long
    
       strTemp = "I bought a NEw phone yesterday."
       For pos = 1 To Len(strTemp) - 1
          lngCharA = Asc(Mid(strTemp, pos, 1))
          lngCharB = Asc(Mid(strTemp, pos + 1, 1))
          If (lngCharA >= 65 And lngCharA <= 90) And (lngCharB >= 65 And lngCharB <= 90) Then
             Mid(strTemp, pos + 1, 1) = LCase(Chr(lngCharB))
          End If
       Next
       MsgBox strTemp
    End Sub

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to detect two uppercase letters.

    StrConv(x,vbProperCase) won't help as it will capitalize the first letter of all the words.

    Instead you must loop thru the string, set a flag when a capital letter is found. So when you get the second capital letter and you see that the flag is true you know you need to replace it.

    This code might help (not tested).
    Note that this is not the most efficient code and there could be better methods for doing this too.
    vb Code:
    1. Function ReplaceTwoCapitalsWithOne(ByVal sourceString as String)
    2.     Dim found as Boolean
    3.     For i = 0 To Len(sourceString) -1
    4.         If Mid(sourceString, i, 1) like "[A-Z]" Then
    5.             If found Then Mid(sourceString, i, 1) = Lcase(Mid(sourceString, i, 1))
    6.             found = True
    7.         Else
    8.             found = False
    9.         End If
    10.     Next
    11.     ReplaceTwoCapitalsWithOne = sourceString
    12. End Sub

    Pradeep
    Last edited by Pradeep1210; Apr 17th, 2008 at 11:13 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to detect two uppercase letters.

    Try this, wrote it really quick:

    Code:
    Private Sub Text1_Change()
        On Error Resume Next
        
        Dim bytCur As Integer, lonLen As Long
        Dim lonSel As Long, lonPrevSpace As Long
        Dim strText As String
        
        strText = Text1.Text
        lonLen = Len(strText)
        lonSel = Text1.SelStart
        
        If lonLen > 0 Then
        
            If lonSel < 2 Then
                bytCur = CByte(Asc(Mid$(strText, 1, 1)))
            Else
                bytCur = CByte(Asc(Mid$(strText, lonSel - 1, 1)))
                lonPrevSpace = InStrRev(strText, " ", lonSel)
            End If
            
            If lonSel > 0 Then
                If lonPrevSpace + 1 <> (lonSel - 1) Then
                    If bytCur >= 65 And bytCur <= 90 Then
                        Mid$(strText, lonSel - 1, 1) = Chr$(bytCur + 32)
                        Text1.Text = strText
                        Text1.SelStart = lonSel
                    End If
                End If
            End If
            
        End If
    End Sub

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    Re: How to detect two uppercase letters.

    Thanks, I used DigiRev's and it worked .

    Resolved

  8. #8
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] How to detect two uppercase letters.

    Another option:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      Select Case KeyAscii
      Case 65 To 90 ' A to Z
        With Text1
          If .SelStart > 0 Then
            Select Case Asc(Mid$(.Text, .SelStart, 1))
            Case 65 To 90: KeyAscii = KeyAscii + 32
            End Select
          End If
        End With
      End Select
    End Sub

  9. #9
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: [RESOLVED] How to detect two uppercase letters.

    Quote Originally Posted by Logophobic
    Another option:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      Select Case KeyAscii
      Case 65 To 90 ' A to Z
        With Text1
          If .SelStart > 0 Then
            Select Case Asc(Mid$(.Text, .SelStart, 1))
            Case 65 To 90: KeyAscii = KeyAscii + 32
            End Select
          End If
        End With
      End Select
    End Sub
    Much better. I tend to write sloppy and sometimes redundantly when I'm tired or rushing through it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width