views:

45

answers:

3

So I have a string "NEW".

What is the SIMPLEST way to convert that string to "New".

Basically right now I'm doing this:

Case "NEW"
    makes = connector.GetMakesByYear(_AuthorizationKey, "NewCar", CDate(Now), Year)
Case "USED"
    makes = connector.GetMakesByYear(_AuthorizationKey, "UsedCar", CDate(Now), Year)

And I would prefer not to use a case statement because it's only one parameter that needs to change, and both are appended with "Car".

+1  A: 

Using the “old” string functions, you can use this:

result = StrConv("hello world", VbStrConv.ProperCase)

to convert a string to “proper case”. However, in your case this would probably result in (if I read this right) “Usercar”, not “UserCar”.

Konrad Rudolph
I believe Scott wants to apply the capitalization only to "New" or "Used" as a variable and then append "Car" to that variable
GôTô
Correct. So this should work for that.
Scott
A: 

You may use:

String.Format("{0}{1}", carType.Substring(0, 1).ToUpper(), carType.Substring(1).ToLower())

Regards

gjsduarte
A: 

If this is something you plan on using often, you might consider creating an extension function for it:

Public Module ObjectExtensions
    <System.Runtime.CompilerServices.Extension()>
    Public Function firstLetterToUpper(ByVal s As String) As String
        Return Char.ToUpper(s.First()) + New String(s.Skip(1).Select(Function(x) Char.ToLower(x)).ToArray())
    End Function
End Module

Then you can do something like this:

"USED".firstLetterToUpper()

Which returns "Used"

Obviously you can change the function body with something more efficient like Guilherme's or Konrad's answer, but making an extension function for this can be quite useful if you do plan on doing something like this often, or if you are just a fan of readability.

diceguyd30