Home > ASP > Making Uppercase Each Word or First Letter of a Sentence in ASP

Making Uppercase Each Word or First Letter of a Sentence in ASP

One of the most disturbing problems of webmasters is users which have no idea about typing a sentence. It’s should be so difficult for website owner’s to see, some silly sentences typed incorrectly on their well-designed and good-looking websites. As we all know each word of titles, names and surnames or the first letter of a sentence ALWAYS start with an uppercase letter. Unfortunately there is no such rule in caveman literature. They write the first letters of sentences or their first letter of their names small. However, you don’t need to get angry anymore. There is a little solution for these cavemen. We are going to write a function that will change first letters of each word in a sentence or the first letter of a sentence. It means, we will set the cavemen’s sentences in a correct sentence format. Also, we can convert the first letter of lines to uppercase. This can be useful when you write a poem, lyric or something like that. Let’s begin.


Firstly, there is our UppercaseTheFirstLetter() function then we will clarify it with examples.

Function 1.0: No Cavemen No Cry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%
function UppercaseTheFirstLetter(string, object)

if object=0 Then 'if first letter of words in sentence must be uppercased.
          splitby = " "
elseif object=1 Then 'if first letter of sentence must be uppercased.
          splitby = "."
elseif object=2 Then 'if first letter of a new line must be uppercased. (ie: poem, lyric etc.)
          splitby = "<br>"
end if

'These keywords can be changed or added manually.  

string=trim(string) 'we removed unnecessary spaces at the beginning and ending of string.
parts = Split(string,splitby) 'string is split by our intercept criteria.
'parts is an array that contains each piece of divided string.

for fi=0 to uBound(parts) 'loop 0 to size of parts array.
the_part=trim(parts(fi)) 'we removed extra spaces at the beginning and ending of part.
if len(the_part)>0 Then 'if the part is not null or not space.
uppercased = ucase(mid(the_part,1,1))&mid(the_part,2,len(the_part)-1)
'The first letter of the_part is taken and converted to uppercase,
'Rest of the_part is added at the end of the letter.
if fi>0 then output=output&splitby&uppercased else output=uppercased'string is gathered again.
end if
next
UppercaseTheFirstLetter=output
end function
%>


At least, we solve it. You can use this function to format your sentences or titles.

Usage of the UppercaseTheFirstLetter () Function

Example 1.0: Uppercase each word of a title.

1
2
3
4
<%
my_title="advanced                          programming solutions"
Response.Write UppercaseTheFirstLetter(my_title,0)
%>

Output of Example 1.0:

1
Advanced Programming Solutions

Example 1.1: Uppercase the first letter of a sentence.

1
2
3
4
<%
my_sentence="we split the sentences by dots. we hope they wrote a dot at the end of the sentence."
Response.Write UppercaseTheFirstLetter(my_sentence,1)
%>

Output of Example 1.0:

1
We split the sentences by dots.We hope they wrote a dot at the end of the sentence.

Example 1.2: Uppercase the first letter of a new line.

1
2
3
4
5
6
7
8
9
10
11
12
<%
my_lyric="i can't run anymore<br>"
my_lyric=my_lyric&"i fall before you<br>"
my_lyric=my_lyric&"here I am<br>"
my_lyric=my_lyric&"i have nothing left<br>"
my_lyric=my_lyric&"though I've tried to forget<br>"
my_lyric=my_lyric&"you're all that I am<br>"
my_lyric=my_lyric&"take me home<br>"
my_lyric=my_lyric&"i'm through fighting it"

Response.Write UppercaseTheFirstLetter(my_lyric,2)
%>

Output of Example 1.0: One of my favorites October from Evanescence.

1
2
3
4
5
6
7
8
I can't run anymore
I fall before you
Here I am
I have nothing left
Though I've tried to forget
You're all that I am
Take me home
I'm through fighting it

Whoever try to stop them, there is nothing to stop a caveman but, we can prepare small handicaps for them like this function. Do not lose your faith. One day, all cavemen will understand that to be a caveman is not a pretty thing. Until that day, good luck.

An Addition: You may want to look at this article that shows how to make user-defined functions in ASP. So you can write your own functions.

Bookmark and Share
eXTReMe Tracker