Re: Surprise in StrConv using vbProperCase

From: Brendan Reynolds (brenreyn)
Date: 11/09/04


Date: Tue, 9 Nov 2004 19:39:23 -0000


>From memory, I believe you're right about Split - that it will include an
empty element if it finds two consecutive delimiters with nothing between
them. Something like the following should eliminate multiple spaces though.
This is untested air-code, so could potentially include typos or syntax
errors on my part, but I think you'll get the gist of it ...

Do While InStr(1, YourString, Space$(2)) > 0
    YourString = Replace(YourString, Space$(2), Space$(1))
Loop

-- 
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com
The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
"Gary Schuldt" <garyschuldt@comcast.net> wrote in message 
news:uCqdl6oxEHA.3572@TK2MSFTNGP10.phx.gbl...
> Thanks, Ken,
>
> (Before posting, I searched for StrConv and proper case and vbProperCase 
> and
> got no hits on any of them.)
>
> Your code will capitalize the first letter of a string.  The strings I 
> have
> could consist of multiple words (they are cultivar names of plants, such 
> as
> MacTavish's Sky Pencil), so the specs for StrConv using vbProperCase match
> what I need.  (Unfortunately, it doesn't work as advertised.)
>
> Because the cultivar often includes the cultivator's name, you could
> conceivably have Meyers-Briggs Delight.
>
> The Int'l Rules for Botanical Nomenclature say each word in the cultivar
> name must be capitalized.   There is no rule to cover the case when 
> hyphens
> are present, so I will just have to trust the user to type the correct 
> name
> in those cases.
>
> I'm thinking of writing a StrCapitalize that will:
>
>    1.  Do what your CapFirstLetter below does, but for each "word" in the
> string;
>    2.  Assume/use a blank as a delimiter for words;
>    3.  Squeeze out superfluous blanks (since they are so hard to detect
> visually).
>
> My pseudocode would be something like:
>
>    1.  Use Split(string into array of words using blank as delimiter);
>    2.  (I'm not what sure Split does when it encounters two consecutive
> delimiters; does it yield an (extra) array element with a value of a
> zero-length string?)
>    3.  Use your CapFirstLetter function on each element of the array
>    4.  Use Join(the array back into a string using blank as delimiter).
>
> I would display the result and tell the user if it's not yet correct (like
> intracaps needed for MacDonald and O'Hara and Barr-Mason) they can correct
> it themselves.
>
> What do you think?
>
> Gary
>
> "Ken Snell [MVP]" <kthsneisllis9@ncoomcastt.renaetl> wrote in message
> news:u793UemxEHA.2348@TK2MSFTNGP12.phx.gbl...
>> Try this (the behavior you note for StrConv is a commonly discussed issue
>> here in the newsgroups):
>>
>> Public Function CapFirstLetter(strString As String) As String
>> Dim strLetter As String
>> strLetter = Left(strString, 1)
>> Select Case Asc(strLetter)
>>     Case 97 To 122
>>         strLetter = Chr(Asc(strLetter) - 32)
>> End Select
>> CapFirstLetter = strLetter & Mid(strString, 2)
>> End Function
>> --
>>
>>         Ken Snell
>> <MS ACCESS MVP>
>>
>>
>>
>> "Gary Schuldt" <garyschuldt@comcast.net> wrote in message
>> news:%23Jk0epjxEHA.1452@TK2MSFTNGP11.phx.gbl...
>> > VBA Help says using the vbProperCase option in the StrConv function
>> converts
>> > the first letter of every word  to uppercase.
>> >
>> > The surprise is that it converts all other characters to lower case!
>> >
>> > This is an *unpleasant* surprise in my application, because it changes
>> > "O'Malley" to "O'malley", which I don't want.
>> >
>> > I just want a function to capitalize the first letter of each word and
>> leave
>> > the other letters alone.  Anyone know of such a function so I don't 
>> > have
>> to
>> > code it myself?
>> >
>> > Gary
>> >
>> >
>>
>>
>
> 


Relevant Pages

  • Re: Encoding Question
    ... > You receive a byte array from a socket. ... > a string with the following function, then splitting the string by the ... > delimiter sub-string. ... I have a similar problem reading in a file that can have various delimiters, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ubound behavior with Split vs. ReDim
    ... > Dim Array() as String ... TextToSplit = Replace$(TextToSplit, Delimiter, Chr$(1)) ...
    (microsoft.public.vb.general.discussion)
  • Re: passing a string to a C++ function
    ... identity function which adds the emails to the encryption process. ... //I set this with each item of the array as it loos and call my function ... terminated string, and it returns to me the encrypted message ... //this will ALWAYS be the delimiter ...
    (microsoft.public.vc.language)
  • Re: Programmers unpaid overtime.
    ... tokenizing a string correctly. ... ability to specify a set of delimiter tokens, ... >> postmodern attack on language itself, ... An the scientists, in turn, think a lot of that artistic talk about ...
    (comp.programming)
  • Re: VB vs C#
    ... Dim delimiter As String ... Dim testname As String ...
    (microsoft.public.dotnet.general)