Converting a sentence into CamelCase format in Microsoft VB.NET & ASP.NET


Microsoft VB.NET, ASP.NET and C#.NET lacks the function to allow programmers to convert a string, or even a whole sentence into Camel Case format. Therefore, i have written a microsoft .net functions to do just that. This comes very handy especially in converting a list of names into Camel Case format, or converting a string of addresses and many other applications. You name it. If you do not know what a Camel Case format look like, see the example below:

For instance, we are going to convert the following files into Camel Case format.

Dim Sentence1 As String = "is john smith working in microsoft?"
Dim Sentence2 As String = "michael t. ashby is a GREAT programmer. he is young."
Dim Output1, Output2 As String

Output1 = CCase(Sentence1)
Output2 = CCase(Sentence2)

The converted output will be
Output1 : "Is John Smith Working In Microsoft?"
Output2 : "Michael T. Ashby Is A Great Programmer. He Is Young."



Simply copy and paste the following codes below into your microsoft vb.net, asp.net programming code and you can use it right away.


Public Function CCase(ByVal iString As String) As String
'This function converts a string into "Camel Case" format

Dim oString As String = "" 'initialise output string
Dim iLength As Integer = Len(iString) 'the length of input string
Dim P As Integer = 1 'set pointer to start at character 1
Dim iChar As String 'individual stripped character
Dim UpperFlag As Boolean 'flag to mark upper case is required or not

iString = LCase(iString) 'convert to lower case string
While P <= iLength ' while not end of the string (pointer)

iChar = Mid(iString, P, 1) 'strip each character out of the string one by one
If P = 1 Then 'first character

UpperFlag = True 'change first character to upper case
End If
If iChar = " " Or iChar = "-" Then 'blank field encountered.

UpperFlag = True 'raise the flag to mark the next character as upper case
Else 'non-blank field encountered
If UpperFlag = True Then 'if the flag is raised
iChar = UCase$(iChar) 'change the character to uppercase
UpperFlag = False 'undo raising the flag
End If
End If
oString &= iChar 'reassemble characters back into a string as output

P += 1 'next pointer
End While

Return (oString) 'return the reassembled output string
End Function

Applying Absolute Value In VB.NET with ABS



VB.NET allows the conversion of figures (both positive and negative) into absolute figures easily. You can use the Mathematical Function as shown below

Math.ABS


For example, to change a figure value of -34167 into absolute figures, we can apply the codes as follows :


Dim myValue as Integer

myValue = Math.ABS(-34167)

MsgBox(myValue) 'The output now becomes 34167



That illustrates the use of the in-built Math.Abs function in VB.NET