user11375447
user11375447

Reputation:

How to pick only Capital characters from the string?

I am try to pick capital characters from the string with the help of a function and for loop but i can't figure out how i can do it i try using toUpperCase as you see it in the code but it is not work any idea how i can do it ?

function onlyCapitalLetters(cap){
    var string = "";
    for(var i = 0; i < cap.length; i++){
        if(cap[i] === cap.toUpperCase()){
            string += cap[i];
        }
    }
    return string;
}

onlyCapitalLetters("Apple");

Upvotes: 2

Views: 963

Answers (5)

Swadesh Ranjan Dash
Swadesh Ranjan Dash

Reputation: 542

Please Use Below code to get Capital letter of the sentence :

Demo Code

var str = 'i am a Web developer Student';
var sL = str.length;
var i = 0;
for (; i < sL; i++) {
    if (str.charAt(i) != " ") {
      if (str.charAt(i) === str.charAt(i).toUpperCase()){
        console.log(str.charAt(i));
        break; 
      }
    }
}

Upvotes: 0

StepUp
StepUp

Reputation: 38134

It is possible to use replace method with Regex to eliminate numbers and letters written in lowercase:

let str = 'T1eeeEeeeSssssssTttttt';
let upperCase = str.replace(/[a-z0-1]/g, '')
console.log(upperCase);

Upvotes: 0

Velusamy Venkatraman
Velusamy Venkatraman

Reputation: 736

Can you try like this

function findUpcase(value){
    input = value
    data = ""
    input.split("").map(res => {
        if(res == res.toUpperCase()){
            data = data+ res
        }
    })
    return data
}

console.log( findUpcase("MyNameIsVelu") );
//'MNIV'

Upvotes: 1

trincot
trincot

Reputation: 350310

As noted in comments you need to change cap.toUpperCase() to cap[i].toUpperCase().

But you can do it with just one replace:

console.log('Apple Orange'.replace(/[^A-Z]/g, ""));

Upvotes: 0

Shubham Dixit
Shubham Dixit

Reputation: 1

You can try the regex, with String.prototype.match to return capital letters only:

function onlyCapitalLetters(cap){
    return  cap.match(/[A-Z]/g, "").join(''); // join the array to return a string
}

console.log(onlyCapitalLetters("Apple"));
console.log(onlyCapitalLetters("BUTTerfly"));
console.log(onlyCapitalLetters("LION"));

Upvotes: 4

Related Questions