JazZ
JazZ

Reputation: 4579

Capitalize the string but not first three lettres

I have strings based on airports codes, name and location.

i.e. :

var s = "BRU – Brussels National, Brussels, BELGIUM";

The point here is to capitalize the string to get "Belgium" instead of "BELGIUM" but to keep the first tree letters upperCase.

How can I do this using jQuery or pure JavaScript ?

The output I need :

BRU - Brussels National, Brussels, Belgium

My try (the new string needs to populate a <span> content) :

$("#airport").html(s.toLowerCase()).css('text-transform', 'capitalize');

But the result when the <span> is rendered is :

Bru - Brussels National, Brussels, Belgium

How to keep the first three letters upperCase ?

Any tips are welcome and will be appreciated.

Thank you.

Upvotes: 0

Views: 98

Answers (5)

Sam Blundell
Sam Blundell

Reputation: 11

I think the first answer was on the right track but still made the "BRU" lowercase I've followed the same concept but hopefully got what you are looking for:

var str = "BRU – Brussels National, Brussels, BELGIUM";
var newstr = str.replace(/[^–]*$/, function (letter) {
                        return letter.toLowerCase();
                    }).replace(/\b[a-z](?=[a-z]{2})/g, function (letter) {
                        return letter.toUpperCase();
                    });

First, using regex

/[^–]*$/

I replace all the characters after the " - " with lowercase then also using regex

/\b[a-z](?=[a-z]{2})/g

I use a replace every first character with a uppercase

Hope this helps

Upvotes: 1

Sunil Kumar
Sunil Kumar

Reputation: 3242

I have done by using below code:

HTML:

<span id="ContryCode">Bru – Brussels National, Brussels, BELGIUM</span>
<button id="btnConvert">
Convert To UperCase
</button>
<br/>
<span id="ConvertedCode"></span>

JQuery:

$("#btnConvert").click(function(){
var code=$("#ContryCode").html();
var codeCapital = code.substring(0, 3).toUpperCase();
$("#ConvertedCode").html(codeCapital+""+code.slice(3))
});

It will convert to uppercase starting 3 letters of any string.

DEMO

Hope it will helps you, thanks

Upvotes: 0

Founded1898
Founded1898

Reputation: 977

If you know that the first string always contains three letters, you could do something like this:

var s = "BRU – Brussels National, Brussels, BELGIUM";
$("#airport").html(s.slice(0, 3) + s.slice(3).toLowerCase()).css('text-transform', 'capitalize');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span id="airport"></span>

Upvotes: 1

Vicky_Burnwal
Vicky_Burnwal

Reputation: 981

In javascript,

    var s = "BRU – Brussels National, Brussels, BELGIUM";
         var strToCap = s.split('-')[1];
        var result= '';
        strToCap = strToCap .split(' ');
        for (var chr = 0; chr < strToCap .length; chr++) {
          result+= strToCap [chr].substring(0, 1).toUpperCase() + strToCap [chr].substring(1, strToCap [chr].length).toLowerCase() + ' '
         }
        result = s.split('-')[0] + "-" + result ;

Upvotes: 0

P. Frank
P. Frank

Reputation: 5745

You can use replace() with regex for make this. Please try:

var s = "BRU – Brussels National, Brussels, BELGIUM";
result = s.split("–");
split = result[1].toLowerCase().replace(/\b[a-z]/g, function(letter) {
    return letter.toUpperCase();
});
console.log(result[0] + "-" + split)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 2

Related Questions