Tôi có thể sử dụng == để so sánh các chuỗi trong python không?

Hướng dẫn cách kiểm tra 2 chuỗi bằng nhau hay giống nhau. Tìm hiểu cách tìm sự khác biệt giữa hai chuỗi. Thực hiện các phép so sánh phức tạp và hơn thế nữa

Miguel Brito

·28 tháng 11 năm 2021·12 phút đọc

Theo dõi bản tin của tôi và không bao giờ bỏ lỡ các bài viết sắp tới của tôi

Đặt mua

Chơi bài viết này

Trình duyệt của bạn không hỗ trợ phần tử âm thanh. TỐC ĐỘ1X

So sánh các chuỗi là một nhiệm vụ cơ bản phổ biến đối với bất kỳ ngôn ngữ lập trình nào

Khi nói đến Python, có một số cách để làm điều đó. Cái tốt nhất sẽ luôn phụ thuộc vào trường hợp sử dụng, nhưng chúng tôi có thể thu hẹp chúng thành một số phù hợp nhất với mục tiêu này

Trong bài viết này, chúng tôi sẽ làm chính xác điều đó

Đến cuối hướng dẫn này, bạn sẽ học được

  • cách so sánh các chuỗi bằng toán tử
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    2 và
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    3
  • cách sử dụng toán tử
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    4 để so sánh hai chuỗi
  • cách so sánh các chuỗi bằng toán tử ________ 05, ________ 06, ________ 07 và ________ 08
  • cách so sánh hai chuỗi bỏ qua trường hợp
  • cách bỏ qua khoảng trắng khi thực hiện so sánh chuỗi
  • cách xác định xem hai chuỗi có giống nhau hay không bằng cách khớp mờ
  • cách so sánh hai chuỗi và trả về sự khác biệt
  • cách gỡ lỗi khi so sánh chuỗi không hoạt động

Đi nào

So sánh các chuỗi sử dụng toán tử
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2 và
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
3

Cách đơn giản nhất để kiểm tra xem hai chuỗi có bằng nhau trong Python hay không là sử dụng toán tử

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2. Và nếu bạn đang tìm kiếm điều ngược lại, thì
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
3 chính là thứ bạn cần. Đó là nó

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2 và
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
3 là các toán tử boolean, nghĩa là chúng trả về
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
35 hoặc
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
36. Ví dụ:
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2 trả về
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
35 nếu hai chuỗi khớp nhau và
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
36 nếu ngược lại

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
8

Các toán tử này cũng phân biệt chữ hoa chữ thường, có nghĩa là các chữ hoa được xử lý khác nhau. Ví dụ bên dưới chỉ ra rằng,

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
90 bắt đầu bằng chữ hoa
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
91 trong khi
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
92 bắt đầu bằng chữ thường
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
93. Kết quả là Python trả về
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
36 khi so sánh chúng với
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
5

So sánh các chuỗi sử dụng toán tử
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
4

Một cách khác để so sánh xem hai chuỗi có bằng nhau trong Python hay không là sử dụng toán tử

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
4. Tuy nhiên, kiểu so sánh mà nó thực hiện khác với
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2. Toán tử
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
4 so sánh xem 2 chuỗi có giống nhau không

Trong Python—và trong nhiều ngôn ngữ khác—chúng ta nói hai đối tượng là cùng một thể hiện nếu chúng là cùng một đối tượng trong bộ nhớ

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816

Hình ảnh dưới đây cho thấy ví dụ này sẽ được thể hiện như thế nào trong bộ nhớ

Như bạn thấy, chúng tôi đang so sánh danh tính, không phải nội dung. Các đối tượng có cùng danh tính thường có cùng tham chiếu và chia sẻ cùng một vị trí bộ nhớ. Hãy ghi nhớ điều đó khi sử dụng toán tử

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
4

Comparing strings using the , = operators

Cách thứ ba để so sánh các chuỗi là theo thứ tự bảng chữ cái. Điều này rất hữu ích khi chúng ta cần xác định thứ tự từ điển của hai chuỗi

Hãy xem một ví dụ

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
3

Để xác định thứ tự, Python so sánh các chuỗi char với char. Trong ví dụ của chúng tôi, ba chữ cái đầu tiên giống nhau

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
31, nhưng chữ cái tiếp theo thì không,
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
32 từ
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
33 đứng trước
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
34 từ
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
35

Điều quan trọng cần lưu ý là so sánh này phân biệt chữ hoa chữ thường. Python xử lý chữ hoa và chữ thường khác nhau. Ví dụ: nếu chúng ta thay đổi

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
36 thành
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
37, thì kết quả sẽ khác vì
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
38 đứng trước
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
39

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
9

⚠️ CẢNH BÁO ⚠️. Tránh so sánh các chuỗi biểu thị số bằng các toán tử này. Việc so sánh được thực hiện dựa trên thứ tự bảng chữ cái, khiến

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
30 được đánh giá thành
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
36

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
3

So sánh hai chuỗi bằng cách bỏ qua trường hợp

Đôi khi chúng ta có thể cần so sánh hai chuỗi—một danh sách các chuỗi hoặc thậm chí một từ điển các chuỗi—bất kể trường hợp nào

Đạt được điều đó sẽ phụ thuộc vào bảng chữ cái mà chúng ta đang xử lý. Đối với chuỗi ASCII, chúng ta có thể chuyển đổi cả hai chuỗi thành chữ thường bằng cách sử dụng

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
32 hoặc viết hoa bằng
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
33 và so sánh chúng

Đối với các bảng chữ cái khác, chẳng hạn như tiếng Hy Lạp hoặc tiếng Đức, việc chuyển đổi thành chữ thường để làm cho chuỗi phân biệt chữ hoa chữ thường không phải lúc nào cũng hoạt động. Hãy xem một số ví dụ

Giả sử chúng ta có một chuỗi bằng tiếng Đức có tên là

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
34, có nghĩa là
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
35. Bạn cũng có thể viết cùng một từ mà không có
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
36, trong trường hợp này, từ đó trở thành
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
37. Nếu chúng ta cố gắng viết thường hoặc viết hoa, hãy xem điều gì sẽ xảy ra

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
3

Điều đó xảy ra bởi vì một cuộc gọi đơn giản tới

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
32 sẽ không làm gì với
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
36. Dạng chữ thường của nó tương đương với
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
50 nhưng bản thân
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
36 có dạng và hình dạng giống nhau ở dạng chữ thường hoặc chữ hoa

Cách tốt nhất để bỏ qua chữ hoa chữ thường và thực hiện so sánh chuỗi phân biệt chữ hoa chữ thường hiệu quả là sử dụng

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
52. Theo các tài liệu

Casefolding tương tự như chữ thường nhưng tích cực hơn vì nó nhằm mục đích loại bỏ tất cả các phân biệt chữ hoa chữ thường trong một chuỗi

Hãy xem điều gì xảy ra khi chúng ta sử dụng

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
52 thay thế

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
5

Cách so sánh hai chuỗi và bỏ qua khoảng trắng

Đôi khi bạn có thể muốn so sánh hai chuỗi bằng cách bỏ qua các ký tự khoảng trắng. Giải pháp tốt nhất cho vấn đề này phụ thuộc vào vị trí của khoảng trắng, liệu có nhiều khoảng trắng trong chuỗi hay không, v.v.

Ví dụ đầu tiên chúng ta sẽ xem xét rằng sự khác biệt duy nhất giữa các chuỗi là một trong số chúng có khoảng trắng ở đầu và/hoặc ở cuối. Trong trường hợp này, chúng ta có thể cắt cả hai chuỗi bằng phương pháp

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
54 và sử dụng toán tử
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2 để so sánh chúng

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2

Tuy nhiên, đôi khi bạn có một chuỗi có khoảng trắng ở khắp nơi, bao gồm nhiều khoảng trắng bên trong chuỗi. Nếu đúng như vậy thì

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
54 là không đủ

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
4

Sau đó, cách khác là xóa các khoảng trắng trùng lặp bằng biểu thức chính quy. Phương thức này chỉ trả về các ký tự trùng lặp, vì vậy chúng ta vẫn cần loại bỏ các ký tự đầu và cuối

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
50

Hoặc nếu bạn không quan tâm đến các bản sao và muốn xóa mọi thứ, thì chỉ cần chuyển chuỗi trống làm đối số thứ hai cho

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
57

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
51

Phương pháp cuối cùng và cuối cùng là sử dụng bảng dịch. Giải pháp này là một giải pháp thay thế thú vị cho regex

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
52

Một điều thú vị về phương pháp này là nó cho phép loại bỏ không chỉ khoảng trắng mà cả các ký tự khác như dấu chấm câu.

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
53

Cách so sánh hai chuỗi cho giống nhau [khớp chuỗi mờ]

Một trường hợp sử dụng so sánh chuỗi phổ biến khác là kiểm tra xem hai chuỗi có gần bằng nhau không. Trong nhiệm vụ này, chúng tôi quan tâm đến việc biết chúng giống nhau như thế nào thay vì so sánh sự bằng nhau của chúng

Để dễ hiểu hơn, hãy xem xét một kịch bản khi chúng ta có hai chuỗi và chúng ta sẵn sàng bỏ qua lỗi chính tả. Thật không may, điều đó là không thể với toán tử

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2

Chúng ta có thể giải quyết vấn đề này theo hai cách khác nhau

  • sử dụng
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    59 từ thư viện tiêu chuẩn
  • sử dụng một thư viện bên ngoài chẳng hạn như
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    20

Sử dụng
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
59

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
59 trong thư viện tiêu chuẩn có lớp
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
23 cung cấp phương thức
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
24 trả về thước đo độ tương tự của chuỗi dưới dạng phần trăm

Giả sử bạn có hai chuỗi giống nhau, chẳng hạn như

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
25 và
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
26. Sự khác biệt duy nhất giữa chúng là chữ cái cuối cùng. Hãy tưởng tượng rằng sự khác biệt này đủ nhỏ đối với bạn và bạn muốn bỏ qua nó

Bằng cách sử dụng

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
27, chúng tôi có thể nhận được tỷ lệ phần trăm mà chúng giống nhau và sử dụng số đó để xác nhận xem hai chuỗi có đủ giống nhau không

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
54

Trong ví dụ này,

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
23 cho chúng ta biết rằng hai chuỗi giống nhau 85%. Sau đó, chúng tôi có thể sử dụng số này làm ngưỡng và bỏ qua sự khác biệt

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
55

Có một vấn đề, mặc dù. Ngưỡng phụ thuộc vào độ dài của chuỗi. Ví dụ: hai chuỗi rất nhỏ, chẳng hạn như

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
29 và
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
40 sẽ khác nhau 50%

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
56

Vì vậy, việc thiết lập một ngưỡng phù hợp có thể khó khăn. Thay vào đó, chúng ta có thể thử một thuật toán khác, thuật toán đếm chuyển vị trí của các chữ cái trong một chuỗi. Và tin tốt là, có tồn tại một thuật toán như vậy, và đó là điều chúng ta sẽ thấy tiếp theo

Sử dụng khoảng cách Damerau-Levenshtein

Thuật toán Damerau-Levenshtein đếm số lượng thao tác tối thiểu cần thiết để thay đổi chuỗi này thành chuỗi khác

Nói cách khác, nó cho biết có bao nhiêu lần chèn, xóa hoặc thay thế một ký tự;

Trong Python, chúng ta có thể sử dụng hàm

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
41 từ thư viện
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
20

Hãy xem khoảng cách Damerau-Levenshtein là bao nhiêu cho ví dụ cuối cùng từ phần trước

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
57

Đó là 1. Vì vậy, điều đó có nghĩa là để chuyển đổi

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
43 thành
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
44, chúng ta cần 1 lần thay đổi. Còn ví dụ đầu tiên thì sao?

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
58

cũng là 1. Và điều đó rất có ý nghĩa, sau cùng chúng ta chỉ cần chỉnh sửa chữ cái cuối cùng để chúng bằng nhau

Bằng cách này, chúng tôi có thể đặt ngưỡng dựa trên số lần thay đổi thay vì tỷ lệ

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
59

Cách so sánh hai chuỗi và trả về sự khác biệt

Đôi khi chúng ta biết trước rằng hai chuỗi khác nhau và chúng ta muốn biết điều gì khiến chúng khác nhau. Nói cách khác, chúng tôi muốn có được "sự khác biệt" của họ

Trong phần trước, chúng tôi đã sử dụng

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
59 như một cách để biết liệu hai chuỗi có đủ giống nhau hay không. Mô-đun này thực sự mạnh hơn thế và chúng ta có thể sử dụng nó để so sánh các chuỗi và chỉ ra sự khác biệt của chúng

Điều khó chịu là nó yêu cầu một danh sách các chuỗi thay vì chỉ một chuỗi. Sau đó, nó trả về một trình tạo mà bạn có thể sử dụng để nối thành một chuỗi và in sự khác biệt

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
0

So sánh chuỗi không hoạt động?

Trong phần này, chúng ta sẽ thảo luận về lý do tại sao phép so sánh chuỗi của bạn không hoạt động và cách khắc phục. Hai lý do chính dựa trên kinh nghiệm của tôi là

  • sử dụng toán tử sai
  • có một dấu cách hoặc dòng mới

So sánh các chuỗi bằng cách sử dụng
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
4 thay vì
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
2

Điều này rất phổ biến đối với các nhà phát triển Python mới làm quen. Rất dễ sử dụng sai toán tử, đặc biệt khi so sánh các chuỗi

Như chúng ta đã thảo luận trong bài viết này, chỉ sử dụng toán tử

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
4 nếu bạn muốn kiểm tra xem hai chuỗi có giống nhau không

Có một khoảng trắng ở cuối dòng mới [
>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
49]

Điều này rất phổ biến khi đọc một chuỗi từ hàm

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
500. Bất cứ khi nào chúng tôi sử dụng chức năng này để thu thập thông tin, người dùng có thể vô tình thêm dấu cách

Nếu bạn lưu trữ kết quả từ

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
500 trong một biến, bạn sẽ không dễ dàng nhận ra vấn đề

>>> name = 'John Jabocs Howard'

>>> another_name = name

>>> name is another_name
True

>>> yet_another_name = 'John Jabocs Howard'

>>> name is yet_another_name
False

>>> id[name]
140142470447472

>>> id[another_name]
140142470447472

>>> id[yet_another_name]
140142459568816
1

Giải pháp ở đây là loại bỏ khoảng trắng khỏi chuỗi mà người dùng nhập vào rồi so sánh. Bạn có thể làm điều đó với bất kỳ nguồn đầu vào nào mà bạn không tin tưởng

Phần kết luận

Trong hướng dẫn này, chúng ta đã thấy 8 cách khác nhau để so sánh các chuỗi trong Python và hai lỗi phổ biến nhất. Chúng ta đã thấy cách chúng ta có thể tận dụng các hoạt động khác nhau để thực hiện so sánh chuỗi và cách sử dụng các thư viện bên ngoài để thực hiện khớp chuỗi mờ

điểm chính

  • Sử dụng toán tử
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    2 và
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    3 để so sánh hai chuỗi bằng nhau
  • Sử dụng toán tử
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    4 để kiểm tra xem hai chuỗi có giống nhau không
  • Sử dụng các toán tử
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    5,
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    6,
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    7 và
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    8 để so sánh các chuỗi theo thứ tự bảng chữ cái
  • Sử dụng
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    509 để so sánh hai chuỗi bỏ qua trường hợp
  • Cắt chuỗi bằng các phương thức gốc hoặc biểu thức chính quy để bỏ qua khoảng trắng khi thực hiện so sánh chuỗi
  • Sử dụng
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    59 hoặc
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    511 để kiểm tra xem hai chuỗi có gần bằng nhau không [khớp mờ]
  • Sử dụng
    >>> name = 'John Jabocs Howard'
    
    >>> another_name = name
    
    >>> name is another_name
    True
    
    >>> yet_another_name = 'John Jabocs Howard'
    
    >>> name is yet_another_name
    False
    
    >>> id[name]
    140142470447472
    
    >>> id[another_name]
    140142470447472
    
    >>> id[yet_another_name]
    140142459568816
    
    59 để so sánh hai chuỗi và trả về sự khác biệt
  • So sánh chuỗi không hoạt động?

Đó là nó cho ngày hôm nay, và tôi hy vọng bạn đã học được điều gì đó mới. Hẹn gặp lại bạn lần sau

Các bài viết khác bạn có thể thích

  • Cách chọn giữa isdigit[], isdecimal[] và isnumeric[] trong Python

  • Cách tốt nhất để so sánh hai từ điển trong Python

  • Cách tốt nhất để so sánh hai danh sách trong Python

  • 15 cách dễ dàng để cắt chuỗi trong Python

  • cột trụ. Cách khắc phục "c0209. định dạng một chuỗi thông thường có thể là chuỗi f [xem xét sử dụng chuỗi f]"

    Bạn có thể sử dụng == để so sánh các chuỗi không?

    Bạn không nên sử dụng == [toán tử đẳng thức] để so sánh các chuỗi này vì chúng so sánh tham chiếu của chuỗi, i. e. liệu chúng có phải là cùng một đối tượng hay không. Mặt khác, phương thức equals[] so sánh xem giá trị của các chuỗi có bằng nhau hay không và không phải chính đối tượng.

    Bạn có thể sử dụng toán tử == với chuỗi không?

    Trong Chuỗi, toán tử == được sử dụng để so sánh tham chiếu của các chuỗi đã cho, tùy thuộc vào việc chúng có đang tham chiếu đến cùng một đối tượng hay không. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .

    Điều gì xảy ra nếu bạn sử dụng == cho chuỗi?

    Toán tử == . Nếu hai chuỗi được so sánh với == đề cập đến cùng một bộ nhớ chuỗi, giá trị trả về là đúng; . returns true if the contents being compared refer to the same memory or false if they don't. If two strings compared with == refer to the same string memory, the return value is true; if not, it is false.

    Có thể so sánh hai chuỗi với biểu tượng == không?

    Bạn cũng có thể so sánh hai chuỗi bằng toán tử == . Tuy nhiên, nó so sánh các tham chiếu với các biến đã cho, không phải giá trị.

Chủ Đề