Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Bạn có thể muốn so sánh hai chuỗi để biết loại nào cao hơn hoặc thấp hơn theo thứ tự bảng chữ cái hoặc để xem chúng có bằng nhau không.

Bạn có thể làm điều này theo nhiều cách. Tôi sẽ cho bạn thấy hai người trong số họ trong bài viết này.

1. Cách so sánh các chuỗi bằng cách sử dụng localecompare

  • 1. Cách so sánh các chuỗi bằng cách sử dụng localecompare
  • 2. Cách so sánh các chuỗi bằng toán tử toán học
        • Làm cách nào để hiển thị dữ liệu từ Excel đến HTML?
        • Hướng dẫn dùng terniary trong php
        • Hướng dẫn regex javascript
        • Làm cách nào để lưu một biến trong JavaScript?
        • JavaScript Chọn từ ngẫu nhiên từ mảng
        • Chức năng không được xác định javascript onclick
        • Hướng dẫn dùng pascally trong php
        • Hướng dẫn băm chuỗi javascript
        • Địa chỉ phân chia hướng dẫn trong javascript
        • LCM gồm 3 số trong JavaScript
        • Nhận khóa, giá trị bản đồ javascript
        • Hướng dẫn python
        • Hướng dẫn tạo thành phần trong javascript
        • Hướng dẫn dùng codebase64 trong php
        • Hướng dẫn nguyên mẫu đối tượng JavaScript
        • BậT JavaScript Trong Internet Explorer
        • Biểu thức javascript hướng dẫn
        • Tìm chuỗi trong chuỗi python
        • Hướng dẫn html thoát
        • Hướng dẫn đối tượng bản đồ javascript
        • Hướng dẫn máy tính-javascript github
        • Bạn có thể kết hợp chuỗi và int trong python?
        • Cách viết hoa một mảng trong JavaScript
        • Hướng dẫn javascript lào được gì
        • Hướng dẫn trích dẫn thoát trong PHP
        • Cách tốt nhất để kết hợp các chuỗi trong Python
        • Hướng dẫn dùng quutoe trong php
        • Hướng dẫn dùng $ này javascript
        • HướNG DẫN JavaScript hiển thị không
        • Làm thế nào để bạn thay đổi liên kết trong JavaScript?
        • Hướng dẫn thẻ html thay thế javascript
        • Mô -đun hướng dẫn javascript
        • Hướng dẫn dùng ufeff javaScript
        • Làm cách nào để kích hoạt JavaScript trên PC của tôi?
        • Làm cách nào để truy vấn một tham số trong JavaScript?
        • Làm thế nào để tôi có được Python để chọn một số ngẫu nhiên?
        • Nhận các giá trị duy nhất từ ​​mảng 2D JavaScript

Bạn có thể sử dụng phương pháp localeCompare để so sánh hai chuỗi trong ngôn ngữ hiện tại. Ở đây, cú pháp:

string1.localeCompare(string2)

locaelCompare Trả về:

  • 1 nếu string1 lớn hơn (cao hơn theo thứ tự bảng chữ cái) so với
    const string1 = "hello"
    const string2 = "world"
    
    const compareValue = string1.localeCompare(string2)
    // -1
    
    0
    order) than
    const string1 = "hello"
    const string2 = "world"
    
    const compareValue = string1.localeCompare(string2)
    // -1
    
    0
  • -1 nếu string1 nhỏ hơn (thấp hơn theo thứ tự bảng chữ cái) so với
    const string1 = "hello"
    const string2 = "world"
    
    const compareValue = string1.localeCompare(string2)
    // -1
    
    0
  • 0 nếu string1
    const string1 = "hello"
    const string2 = "world"
    
    const compareValue = string1.localeCompare(string2)
    // -1
    
    0 bằng nhau theo thứ tự bảng chữ cái

Dưới đây là một số ví dụ so sánh hai chuỗi:

const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1

Nó cung cấp

const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
5 bởi vì, trong địa phương tiếng Anh, H hello đến trước w trên thế giới (w ở xa hơn theo thứ tự bảng chữ cái so với h)h in hello comes before w in the world (w is further down in the alphabetical order than h)

Một vi dụ khac:

const string1 = "banana"
const string2 = "back"

const compareValue = string1.localeCompare(string2)
// 1

Sự so sánh ở trên đưa ra

const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
6 bởi vì, tại địa phương tiếng Anh, lệnh cấm chuối đến sau BAC ở phía sau.
because, in the English locale, ban in banana comes after bac in back.

Một ví dụ nữa:

const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1

So sánh về FCC và FCC và FCC, cho

const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
7 vì chúng bằng nhau theo thứ tự. Càng FCC và FCC và FCC cung cấp cho
const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
5 vì vốn có F F F F F F FC.

Trong một số trình duyệt, thay vì -1, nó có thể trả về -2 hoặc một số giá trị âm khác. Vì vậy, không phụ thuộc vào -1 hoặc 1, thay vào đó vào các giá trị âm (nhỏ hơn 0) hoặc dương (nhiều hơn 0)-1, it may return -2 or some other negative value. So, do not depend on -1 or 1, instead on negative
(less than 0) or positive (more than 0) values

2. Cách so sánh các chuỗi bằng toán tử toán học

Bạn cũng có thể sử dụng các toán tử toán học như lớn hơn (>), ít hơn (>), less than (<), and equal to when comparing strings.

Các toán tử toán học hoạt động tương tự như localeCompare - bằng cách trả về kết quả dựa trên thứ tự của các ký tự trong chuỗi.

Sử dụng các ví dụ trước:

const string1 = "hello"
const string2 = "world"

console.log(string1 > string2)
// false

string1 không lớn hơn

const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
0, bởi vì H đến trước W, vì vậy nó ít hơn.h comes before w, so it is less than.

Ví dụ khác:

const string1 = "banana"
const string2 = "back"

console.log(string1 > string2)
// true

string1 lớn hơn

const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
0 vì lệnh cấm đến sau khi trở lại.n comes after back.

Và ví dụ cuối cùng:

const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

console.log(string1 === string2)
// true

console.log(string1 < string3)
// false

string1 bằng (

const string1 = "banana"
const string2 = "back"

const compareValue = string1.localeCompare(string2)
// 1
5)
const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
0, nhưng string1 không nhỏ hơn
const string1 = "banana"
const string2 = "back"

const compareValue = string1.localeCompare(string2)
// 1
8, trái ngược với localeCompare.

Với các toán tử toán học, thì FCC, lớn hơn so với FCC, nhưng với localeCompare,

const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1
1 trả về
const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
5 để cho thấy rằng, FC FCC ít hơn so với FCC.
localeCompare,
const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1
1 returns
const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1
5 to show that “fcc” is less than “Fcc”.

Hành vi này là một lý do tại sao tôi không khuyên bạn nên sử dụng các toán tử toán học để so sánh các chuỗi, mặc dù nó có khả năng làm điều đó.

Một lý do khác tại sao tôi không khuyên bạn nên sử dụng các toán tử toán học là vì

const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1
3 và
const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1
4 là
const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1
5. Cấm FCC trực tiếp bằng với FCC. Vì vậy, nếu bạn phụ thuộc vào các nhà khai thác toán học, nhận được
const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1
5 có thể vì những lý do khác nhau so với bạn tin.
believe.

Vì vậy, để so sánh các chuỗi, trong số nhiều cách có thể có, sử dụng

const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1
7 là một cách tiếp cận hiệu quả vì nó có thể được sử dụng cho các ngôn ngữ khác nhau.

Bây giờ bạn đã biết một cách dễ dàng để so sánh các chuỗi. Mã hóa hạnh phúc!

Học mã miễn phí. Chương trình giảng dạy nguồn mở Freecodecamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Làm cách nào để hiển thị dữ liệu từ Excel đến HTML?

Trong hướng dẫn này, bạn có thể tìm cách đọc tệp Excel bằng JavaScript và hiển thị dữ liệu trang tính Excel trên trang web ở định dạng bảng HTML bằng JavaScript. Trước đây, một trong những hướng dẫn của chúng tôi, trong đó chúng tôi
file using JavaScript and display excel sheet data on web page in HTML table format using JavaScript. In previous, one of our tutorial, in which we …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn dùng terniary trong php

Để lập trình nhanh hơn, trong ngôn ngữ lập trình php, chún ta nên thường xuyênn sử dụng các Có NHững Phép Toán

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn regex javascript

I. Giới thiệuBiểu thức chính quy (regular expressions ) là các mẫu dùng để tìm kiếm các bộ kí tự được kết hợp với nhau trong các chuỗi kí tự. Trong …
tự. Trong …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

How do i save a variable in javascript?

If you mean in a web browser, there are two options:CookiesWeb storage (in your case, specifically, local storage as opposed to session storage)Cookies are universally supported by browsers, …
are two options:CookiesWeb storage (in your case, specifically, local storage as opposed to session storage)Cookies are universally supported by browsers, …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Javascript select random word from array

Its a simple one-liner:const randomElement = array[Math.floor(Math.random() * array.length)]; For example:const months = [January, February, March, April, May, June, July]; const …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Function is not defined javascript onclick

Im trying to make a userscript for a website to add custom emotes. However, Ive been getting a lot of errors.Here is the function:function saveEmotes() { removeLineBreaks(); EmoteNameLines …
emotes. However, Ive been getting a lot of errors.Here is the function:function saveEmotes() { removeLineBreaks(); EmoteNameLines …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn dùng fainally trong PHP

Trong bài này, chúng ta sẽ tìm hiểu về ngoại lệ (exception) trong PHP. Để học tốt bài này, các bạn cần đọc lại bài Cài đặt môi trường lập trình Web PHP …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn hash string javascript

Many of the answers here are the same String.hashCode hash function taken from Java. It dates back to 1981 from Gosling Emacs, is extremely weak, and makes zero sense performance-wise in modern …
from Gosling Emacs, is extremely weak, and makes zero sense performance-wise in modern …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn split address in javascript
address in javascript

Im trying to split street name, house number, and box number from a String. Lets say the string is SomeStreet 59A For this case I already have a solution with regex. Im using this …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Lcm of 3 numbers in javascript

I am using the following code to determine the Greatest Common Denominator (GCD) of two or three numbers:Math.GCD = function(numbers) { for (var i = 1 ; i < numbers.length ; i++){ if …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Get key, value map javascript

The get() method returns a specified element from a Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to …
object, then you will get a reference to that object and any change made to …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn substr python

Nội dung chính2- Python String 2- Truy cập các giá trị trong string 3- String là bất biến (immuable) 4- Các ký tự thoát (Escape Characters) 5- Các toán tử cho string 6- Các …
giá trị trong string 3- String là bất biến (immuable) 4- Các ký tự thoát (Escape Characters) 5- Các toán tử cho string 6- Các …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn create component in javascript

Yesterday, we looked at what web components are. Today, I wanted to share how to actually create one.Let’s dig in!Today’s article is an excerpt from my new course and ebook on web components with …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn dùng codebase64 trong PHP

Chuyển đến nội dungCó nhiều giải pháp để mã hoá hình ảnh, Encode Base64 là một trong số đó. Khi chuyển hình ảnh về dạng này ảnh được hiển thị trực …
Khi chuyển hình ảnh về dạng này ảnh được hiển thị trực …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn object prototype javascript

Khi nghe người ta nói về Javascript, bạn chỉ biết Javascript là ngôn ngữ lập trình hướng sự kiện, kiểu script… Thế nhưng, có một điều thú vị rằng, …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Bật javascript trong internet explorer

Trên menu của trình duyệt web nhấn Tools và chọn Internet Options. Trong cửa sổ Internet Options chọn tab Security. Trên Security tab nhấp vào nút Custom …
cửa sổ Internet Options chọn tab Security. Trên Security tab nhấp vào nút Custom …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn javascript expression

Bài viết được sự cho phép của tác giả Lưu Bình AnGiới thiệuRegular Expression (hay gọi tắt là regex – đọc là ghi-ríc-cờ-sờ) là một string với …
viết được sự cho phép của tác giả Lưu Bình AnGiới thiệuRegular Expression (hay gọi tắt là regex – đọc là ghi-ríc-cờ-sờ) là một string với …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Find string in string python

View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticlePython String find() method returns the lowest index or first occurrence of the substring if it is found …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn html escape

I want to display text to HTML by a JavaScript function. How can I escape HTML special characters in JavaScript? Is there an API? asked Jun 4, 2011 at 4:503Heres a solution that will work in …
solution that will work in …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn map object javascript

Nội dung bài viếtVideo học lập trình mỗi ngàyThông thường thì Array.prototype.map sẽ hỗ trợ Array, vậy nếu là một object thì sẽ sử dụng map như thế …
thì Array.prototype.map sẽ hỗ trợ Array, vậy nếu là một object thì sẽ sử dụng map như thế …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn calculator-javascript github
calculator-javascript github

Here are 92 public repositories matching this topic… Code Issues Pull requests A Calculator App built with HTML, CSS, and JavaScript. It also has a Dark Mode. Updated Sep 17, 2022 CSS …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Can you combine string and int in python?

In this tutorial, you’ll learn how to use Python to concatenate a string and an int (integer). Normally, string concatenation is done in Python using the + operator. However, when working with …
string and an int (integer). Normally, string concatenation is done in Python using the + operator. However, when working with …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

How to uppercase an array in javascript

To recap :There are 2 main methods to do it :1. Using the modern Javascript ES6 version with arrow function and map() iterator :// the test array const items = [item1, item2, item3, item4, …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn javascript làm được gì

Hiện nay, JavaScript là một ngôn ngữ đang được nhiều bạn trẻ quan tâm nhiều trong những năm gần đây. Vậy JavaScript là gì? Và việc Tất cả sẽ có đầy …
JavaScript là gì? Và việc Tất cả sẽ có đầy …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn escape quotes in php

(PHP 4, PHP 5, PHP 7, PHP 8)addslashes — Quote string with slashesDescriptionaddslashes(string $string): stringsingle quote ()double quote ()backslash ()NUL (the NUL byte) A use case of …
string with slashesDescriptionaddslashes(string $string): stringsingle quote ()double quote ()backslash ()NUL (the NUL byte) A use case of …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Best way to concatenate strings in python

The best way of appending a string to a string variable is to use + or +=. This is because its readable and fast. They are also just as fast, which one you choose is a matter of taste, the latter …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn dùng qutoe trong PHP

HTML quote được sử dụng để đặt một đoạn trích dẫn vào website. Để thực hiện, bạn cần sử dụng thẻ q và thẻ blockquote. Nội dung chính Thẻ blockquote …
Nội dung chính Thẻ blockquote …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn dùng $this JavaScript

Bài viết gốc: https://manhhomienbienthuy.github.io/2016/03/28/hieu-va-master-tu-khoa-this-cua-javascript.htmlTừ khóa this của JavaScript là một trong những khái niệm cơ bản …
https://manhhomienbienthuy.github.io/2016/03/28/hieu-va-master-tu-khoa-this-cua-javascript.htmlTừ khóa this của JavaScript là một trong những khái niệm cơ bản …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn javascript display none

Đề bài: Hãy viết ứng dụng ẩn và hiện thẻ div bằng Javascript bằng cách tạo ra 2 button, khi click vào button1 thì ẩn thẻ div và khi click vào button2 thì hiển …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

How do you change the link in javascript?

I have a form where the user inputs an URL. The value resulting from that input after tweaking it a bit is the variable #url which is displayed to the user right away.I need to make the like and …
from that input after tweaking it a bit is the variable #url which is displayed to the user right away.I need to make the like and …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn html tag replace javascript

Phương thức string.replace() có chức năng tìm kiếm một chuỗi con hoặc một biểu thức chính quy nào đó trong chuỗi sau đó thay thế nó bằng một giá trị …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn module javascript

Nếu bạn mới học JavaScript, những thuật ngữ như “module bundlers vs. module loaders,” “Webpack vs. Browserify” và “AMD vs. CommonJS” có thể làm các bạn ‘bội …
có thể làm các bạn ‘bội …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn dùng ufeff JavaScript

Im trying to export String data to CSV using unicode in JavaScript using UTF-8 and uFEFF. The problem is when the CSV file build he take the string after comma , and the first cell filled with this …
JavaScript using UTF-8 and uFEFF. The problem is when the CSV file build he take the string after comma , and the first cell filled with this …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

How do i enable javascript on my pc?

Internet Explorer 9 Internet Explorer 10 Internet Explorer 11 More…Less SummaryMany Internet Web sites contain JavaScript, a scripting programming language that runs on the web browser to make …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

How do i query a parameter in javascript?

Update: Jan-2022Using Proxy() is faster than using Object.fromEntries() and better supportedconst params = new Proxy(new URLSearchParams(window.location.search), { get: (searchParams, prop) => …
Object.fromEntries() and better supportedconst params = new Proxy(new URLSearchParams(window.location.search), { get: (searchParams, prop) => …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

How do i get python to pick a random number?

This article is part of in the series Published: Wednesday 15th February 2017Last Updated: Thursday 30th December 2021The aim of a programmer while creating a program is to ensure that the program …

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Hướng dẫn compare two string javascript - so sánh hai chuỗi javascript

Get unique values from 2d array javascript

If you can use lodash, this is good solution (note that based on your platform, the require keyword can differ, this is how to use it in Node.js):var _ = require(lodash); var objects = [[a, …
based on your platform, the require keyword can differ, this is how to use it in Node.js):var _ = require(lodash); var objects = [[a, …