String Conversion - Capitals to Upper and LowerCase

BPak

Active Member
Licensed User
Longtime User
I have a string which is in capitals "JIM O'CONNOR" and I want to convert it to

"Jim O'Connor"

Is there any special Libraries or Functions for doing this?
 

Theera

Well-Known Member
Licensed User
Longtime User
I have a string which is in capitals "JIM O'CONNOR" and I want to convert it to

"Jim O'Connor"

Is there any special Libraries or Functions for doing this?

Hi,
Try to use Margret 's module Manage String. I think you can serach.
Best Regards
Best Regards
Theera
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You can use this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)

    Dim s As String

    s = ToMixCase("some string value")

    MsgBox(s, "")

End Sub


Sub ToMixCase(Entry As String) As String

    Dim sb As StringBuilder
    Dim m As Matcher
    Dim I As Int
            
    Entry = Entry.ToLowerCase

    sb.Initialize
    
    m = Regex.Matcher("(^\w)|(\s\w)", Entry)
    
    Do While m.Find
            
       If m.Match.Length > 1 Then    
    
          sb.Append(Entry.SubString2(I, m.GetStart(0) + 1))
          sb.Append(m.Match.SubString(1).ToUpperCase)
                              
       Else
      
          sb.Append(Entry.SubString2(I, m.GetStart(0)))
          sb.Append(m.Match.ToUpperCase)
                              
       End If
                     
       I = m.GetEnd(0)
                     
    Loop
            
    If I < Entry.Length Then
            
       sb.Append(Entry.SubString(I))
                     
    End If
            
    Return sb.ToString

End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
This can be very useful in converting a COLUMN in a SQLite table to where each word would start with upper case, namely a column that has a first and last name. NJ, you have an uncanny string manipulation ability.
B4X:
Dim txt, s, sOrig As String
Dim i as Int
      Cursor1=SQL1.ExecQuery("SELECT MyCol FROM MyTable")
      Cursor1.Position=0
      For i=0 To Cursor1.RowCount-1
         Cursor1.Position=i
         sOrig=Cursor1.GetString("MyCol")
         s=ToMixCase(sOrig)   'executes NJDude's sub
         txt="UPDATE MyTable SET MyCol = ? WHERE MyCol = ?"
         SQL1.ExecNonQuery2(txt,Array As String(s,sOrig))
      Next
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
You can also use some InputTypes for Word and Sentence case that may help in formatting these from the start when the user types their name. For some reason Erel didn't include them and a few others in the Class, but you can make them yourself. They are in Google Docs, or my View Manager class has some "Constants" that are pre-made to assign to the Input Type you can grab.
 
Upvote 0
Top