简体   繁体   中英

How can I check if a letter in a string is uppercase using the power of TypeScript?

My question is very brief. I am new to TypeScript, been searching around here and there but didn't find yet an answer.

I have this code written in C#

private bool MaskValidateChar(KeyPressEventArgs e, int index)
{
    string c = e.KeyChar.ToString();
    if (Char.IsUpper(c[0])) //Need to this with TypeScript :-\
    {
        //Do Something....
    }
}

When I convert above code into Type script, I can simply write a code something like if (c[0] == c[0].toUpperCase())

I just need to know whether there is a built in method in Typescript to check whether a given character is uppercase. I couldn't find such thing on the internet, but I doubt it.

Please advise.

No. JavaScript (which is what TypeScript compiles to) does not have a built-in method similar to char.IsUpper/char.IsLower . You will have to compare it like:

c[0] === c[0].toUpperCase() // c[0] is uppercase
c[0] === c[0].toLowerCase() // c[0] is lowercase

yes.you can try with linq

if (yourString.Any(char.IsUpper) &&
    yourString.Any(char.IsLower))

Expanding on the answer from @Saravana, TypeScript's type-checking doesn't exist at run-time, it's a check at edit/transpile-time. This means that exceptions can't be automatically raised based solely on the type or contents of a variable. Missing features will cause errors, but that only works if you're using a function exclusive to the variable type you're targeting (of which there are none specifically for uppercase/lowercase strings).

Options? Well, if you know you're dealing with a specific set of possible strings, you can set up a type :

type UCDBOperation = "INSERT" | "DELETE";
type LCDBOperation = "insert" | "delete";

function DoDBOperation(operation: UCDBOperation): void { ... }

const someUCOperation: UCDBOperation = ...;
const someLCOperation: LCDBOperation = ...;

DoDBOperation("INSERT");        // no error!
DoDBOperation(someUCOperation); // no error!
DoDBOperation("insert");        // type error
DoDBOperation(someLCOperation); // type error
DoDBOperation("fakeoperation"); // type error
DoDBOperation("FAKEOPERATION"); // type error

If you're only concerned with single alpha characters this can be taken a step further:

type UCAlpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z";

function printUpperCaseLetter(letter: UCAlpha): void {
  console.log(letter);
}

printUpperCaseLetter("A");     // no error!
printUpperCaseLetter("a");     // type error
printUpperCaseLetter("hello"); // type error
printUpperCaseLetter("WORLD"); // type error

Just be careful of user-generated strings . Any data generated at run-time won't have this type-checking on it:

// Typescript has no idea what the content
// of #SomeTextField is since that data
// wasn't available at transpile-time
DoDBOperation(document.querySelector("#SomeTextField").textContent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM