Home > JavaScript > Making Uppercase First Letters of Words, Sentences or Lines in JavaScript

Making Uppercase First Letters of Words, Sentences or Lines in JavaScript

We have talked about making uppercase the first letters of words, sentences and lines in ASP before. Now, we will make the same application in JavaScript.


Let’s look at the UppercaseTheFirstLetter() function.

Function 1.0: UpperFunction.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function UppercaseTheFirstLetter(string,option){
var splitby,parts,i,uppercased;
if(option==0){splitby=" ";}
else if(option==1){splitby=".";}
else if(option==2){splitby="<BR>";}

string=string.replace(/^\s+/, '').replace(/\s+$/, '');
//we removed unnecessary spaces at the beginning and ending of string.
parts = string.split(splitby);

for(i=0;i<parts.length;i++){
          parts[i]=parts[i].replace(/^\s+/, '').replace(/\s+$/, '');
          if(parts[i].length>0){
          uppercased=parts[i].slice(0,1).toUpperCase()+parts[i].slice(1,parts[i].length);
                    if(i>0)output+=splitby+uppercased; else output=uppercased;
          }
}

return output;
}

This function is an avoid function that makes the first letters of all separated words, sentences or lines uppercased, then returns the string.

Usage of the UppercaseTheFirstLetter () Function

Example 1.0: Converting first letters of words in sentences to uppercase.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
<script type="text/javascript" src="UpperFunction.js"></script>
<script>
function MakeMyContentUpper(){
var content=document.getElementById("my_example_content");
content.innerHTML=UppercaseTheFirstLetter(content.innerHTML,0);
}
</script>

<body onload="MakeMyContentUpper();">

<div id="my_example_content">
programming became easier and funnier with mEMISO.
</div>

</body>
</html>

Output of Example 1.0:

1
Programming Became Easier And Funnier With MEMISO.

Example 2.0: Converting first letter of sentences to uppercase.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<script type="text/javascript" src="UpperFunction.js"></script>
<script>
function MakeMyContentUpper(){
var content=document.getElementById("my_example_content");
content.innerHTML=UppercaseTheFirstLetter(content.innerHTML,1);
}
</script>

<body onload="MakeMyContentUpper();">

<div id="my_example_content">
i have watched the matrix movie.keanu reeves was awesome!</div>
</body>
</html>

Output of Example 2.0:

1
I have watched the matrix movie.Keanu reeves was awesome!

Example 3.0: Converting to uppercase first letter of lines.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<script type="text/javascript" src="UpperFunction.js"></script>
<script>
function MakeMyContentUpper(){
var content=document.getElementById("my_example_content");
content.innerHTML=UppercaseTheFirstLetter(content.innerHTML,2);
}
</script>

<body onload="MakeMyContentUpper();">

<div id="my_example_content">
i have watched the matrix movie.<br/>keanu reeves was awesome.<br/><br>you think that's air you breathing now!</div>
</body>
</html>

Output of Example 3.0:

1
2
3
I have watched the matrix movie.
Keanu reeves was awesome.
You think that's air you breathing now!

Bookmark and Share
  1. No comments yet.
  1. No trackbacks yet.
eXTReMe Tracker