最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 24 String Comparison with Ignore Case - Stack Overflow

programmeradmin8浏览0评论

I'm paring 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>

I'm paring 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>
Share Improve this question edited Jan 24, 2018 at 16:11 Felix Too asked Jan 24, 2018 at 16:08 Felix TooFelix Too 11.9k5 gold badges26 silver badges25 bronze badges 1
  • Possible duplicate of JavaScript case insensitive string parison – Supamiu Commented Jan 24, 2018 at 16:14
Add a ment  | 

4 Answers 4

Reset to default 8

You should use === with toLowercase()

<div *ngIf="query?.firstName.toLowerCase() === address.foreName.toLowerCase()"></div>

Use Angular pipe.

<div *ngIf="(query?.firstName | lowercase) === (address.foreName | lowercase)"></div>

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')

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

 apre(){
    let str1 = "Wele to Xyx World";
    let str2 = "world";
    if(str1.toLowerCase().includes(str2.toLowerCase())){
      console.log("Found");
    }else{
      console.log("Not Found");
    }
  }
发布评论

评论列表(0)

  1. 暂无评论