letter convertion

Posted on 2006-05-15
Last Modified: 2009-12-16

1] how can i convert the first and last letter of a word to uppercase with javascript.



2] how to convert the first letter to uppercase with javascript.



3] and how convert the last letter to uppercase with javascript.



please advice with code for all three samples apart
Question by:eaweb
    LVL 17

    Expert Comment

    I don't know exactly how to convert a character to uppercase using character codes, but I do know how to convert a string to uppercase!

    <script type="text/javascript">
    var str = "smart";
    document.write("Original: " + str);
    var str1 = str.substr(0,1);
    var str2 = str.substr(1,str.length-2);
    var str3 = str.substr(str.length-1, 1);
    var result1 = str1.toUpperCase() + str2 + str3.toUpperCase();
    var result2 = str1.toUpperCase() + str2 + str3;
    var result3 = str1 + str2 + str3.toUpperCase();
    document.write("1: " + result1);
    document.write("2: " + result2);
    document.write("3: " + result3);

    str1 holds the first letter
    str2 holds the middle
    str3 holds the last letter

    I started with the all lowercase string "smart".  The middle and ends remain UNCHANGED but for the toUpperCase() requirements.
    You could actually specify toLowerCase() if that be the case for parts you do no want capitalized.

    Joe P
    LVL 6

    Expert Comment

    BogoJoker has a good approach, but watch out for short strings.  For example, you probably want "i" to become "I" but the routine above might crash at str.substr(1, str.length-2) since str.length-2 will be negative.
    LVL 17

    Accepted Solution

    Good Point William Frantz!
    You can add an extra check at the top:

    if (str.length == 1)
      // Capitalize this single character
    if (str.length == 2)
     // Do as you wish with this

    Then the rest is normal.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now