Link to home
Start Free TrialLog in
Avatar of dwe0608
dwe0608Flag for Australia

asked on

URGENT: String Conversion

Hi Guys

(1) I have a string as follows:

ThisIsAnExampleString

I am looking for a function to convert that string to:

This Is An Example String

(2) I also have a string as follows:

This_Is_An_Example_String

I would like to convert that string to the same as in my first question ... I can do this using the Replace Function in VB6; but I would like to use one function to do both conversions ....

Hoping my question is clear

MTIA


Darrin


ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bingie
bingie

Private Sub Form_Load()
    MsgBox convertString("ThisIsAnExampleString")
    MsgBox convertString("This_Is_An_Example_String")
End Sub

Private Function convertString(str As String) As String

    If InStr(str, "_") > 0 Then
        str = Replace(str, "_", " ")
        Exit Sub
    End If
   
    'Since capital letters are ascii 65 to 90,
    'we can check for that
    Dim cnt As Integer
    For cnt = 2 To Len(str) 'Start at the second letter
        If (Asc(Mid(str, cnt, 1)) >= 65 And Asc(Mid(str, cnt, 1)) <= 90) Then
            'The letter is a capital
            str = Mid(str, 1, cnt - 1) & " " & Mid(str, cnt, 1) & Mid(str, cnt + 1)
            cnt = cnt + 2
        End If
    Next
       
    convertString = str

End Function
Avatar of dwe0608

ASKER

Guy, both answers work ... brilliant, now, how could we resolve the string if it were

This_IsAnExample_String

ie its combination of both questions ...

MTIA

Darrin

My solution should work in that case as well...
Avatar of dwe0608

ASKER

Thasnks for the prompt help guys ... muchly appreciated ....
You could use API to check the bit of the character, if your format of the start of a new word is a capital letter than it would work perfectly. Let me know if you would like to see an example using this method.
Avatar of dwe0608

ASKER

Always willing to see examples egl1044 ....

MTIA

Darrin
Avatar of dwe0608

ASKER

Sirbounty

If I have a string, AnITGuru .... it would under the current rules of the function be broken down to

An I T Guru

is it possible to change the coding so if we have two or more capitals in a row it only seperates the last one ?

So if we had

AnEXTRAA as the string - it would come out

An EXTRA A

And perhaps if string were "IAmEmployedByI.T.S.A" - it sould say

I Am Employed By I.T.S.A

I am willing to post another question - but did know how to follow on from here ... so if you have the answer, I will post a question for you for 500 points ...

MTIA

Darrin
The first request wouldn't be too terrible complicated, but the others add a bit more to the logic.
You can open a new question, and in the body of the question, paste the link to this question, stating it's a followup.
I'll see if I can come up with something to do what you ask...