How to case-insensitively check if a character exists in a string with TypeScript

2 Answers

0 votes
const str = 'typescript c c++';
const ch = 'T';

console.log(str.toLowerCase().includes(ch.toLowerCase()));
console.log(str.includes('T')); 




/*
run:

true
false

*/

 



answered Feb 6, 2022 by avibootz
0 votes
// Case-insensitive check without a loop
function charExistsIgnoreCase(s: string, target: string): boolean {
     return s.toLowerCase().includes(target.toLowerCase());
}

// Define the string we want to search in
const s: string = "TypeScript";

// Perform the case-insensitive check
const exists: boolean = charExistsIgnoreCase(s, "t");

// Print the raw boolean result
console.log(exists);

// Conditional check
if (exists) {
    console.log("exists");
} else {
    console.log("not exists");
}


/*
run:

true
exists

*/

 



answered 5 hours ago by avibootz
edited 4 hours ago by avibootz

Related questions

...