5

I'm comparing a number of fields in an Angular 2 template and it works for same case properties but returns false for the same string on different cases. Is there a way to make it case insensitive perhaps through a simple pipe?

<div *ngIf="query?.firstName == address.foreName"></div>
Felix Too
  • 11,614
  • 5
  • 24
  • 25
  • Possible duplicate of [JavaScript case insensitive string comparison](https://stackoverflow.com/questions/2140627/javascript-case-insensitive-string-comparison) – Supamiu Jan 24 '18 at 16:14

4 Answers4

6

You should use === with toLowercase()

<div *ngIf="query?.firstName.toLowerCase() === address.foreName.toLowerCase()"></div>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
6

Use Angular pipe.

<div *ngIf="(query?.firstName | lowercase) === (address.foreName | lowercase)"></div>
Jecfish
  • 4,026
  • 1
  • 18
  • 16
1

If you are doing lot of string operations in your application, I suggest using a third party library like this one -

How to install

npm install --save string

Import

var S = require('string');

Ignorecase Compare String

var isEqual = S('ignoreCase').equalsIgnoreCase('IGNORECASE')
Akash
  • 4,412
  • 4
  • 30
  • 48
0

Convert Both string into lowercase and then compare. Find the below code.

 comapre(){
    let str1 = "Welcome to Xyx World";
    let str2 = "world";
    if(str1.toLowerCase().includes(str2.toLowerCase())){
      console.log("Found");
    }else{
      console.log("Not Found");
    }
  }