Hướng dẫn find the difference between two strings javascript - tìm sự khác biệt giữa hai chuỗi javascript

Một lựa chọn khác, để kiểm tra sự khác biệt tinh vi hơn, là sử dụng thuật toán patienceiff. Tôi đã chuyển thuật toán này đến JavaScript tại ...

https://github.com/jonTrent/PatienceDiff

... Mặc dù thuật toán thường được sử dụng để so sánh từng dòng của văn bản (như chương trình máy tính), nhưng nó vẫn có thể được sử dụng để so sánh ký tự. Ví dụ, để so sánh hai chuỗi, bạn có thể làm như sau ...

let a = "thelebronnjamist";
let b = "the lebron james";

let difference = patienceDiff( a.split(""), b.split("") );

... với difference.lines được đặt thành một mảng với kết quả so sánh ...

difference.lines: Array(19)

0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}

Bất cứ nơi nào aIndex === -1 hoặc bIndex === -1 là một dấu hiệu cho thấy sự khác biệt giữa hai chuỗi. Đặc biệt...

  • Phần tử 3 chỉ ra rằng ký tự "" đã được tìm thấy trong b ở vị trí 3.
  • Phần tử 10 chỉ ra rằng ký tự "N" đã được tìm thấy trong
    difference.lines: Array(19)
    
    0: {line: "t", aIndex: 0, bIndex: 0}
    1: {line: "h", aIndex: 1, bIndex: 1}
    2: {line: "e", aIndex: 2, bIndex: 2}
    3: {line: " ", aIndex: -1, bIndex: 3}
    4: {line: "l", aIndex: 3, bIndex: 4}
    5: {line: "e", aIndex: 4, bIndex: 5}
    6: {line: "b", aIndex: 5, bIndex: 6}
    7: {line: "r", aIndex: 6, bIndex: 7}
    8: {line: "o", aIndex: 7, bIndex: 8}
    9: {line: "n", aIndex: 8, bIndex: 9}
    10: {line: "n", aIndex: 9, bIndex: -1}
    11: {line: " ", aIndex: -1, bIndex: 10}
    12: {line: "j", aIndex: 10, bIndex: 11}
    13: {line: "a", aIndex: 11, bIndex: 12}
    14: {line: "m", aIndex: 12, bIndex: 13}
    15: {line: "i", aIndex: 13, bIndex: -1}
    16: {line: "e", aIndex: -1, bIndex: 14}
    17: {line: "s", aIndex: 14, bIndex: 15}
    18: {line: "t", aIndex: 15, bIndex: -1}
    
    0 ở vị trí 9.
  • Phần tử 11 chỉ ra rằng ký tự "" đã được tìm thấy trong b ở vị trí 10.
  • Phần tử 15 chỉ ra rằng ký tự "I" đã được tìm thấy trong
    difference.lines: Array(19)
    
    0: {line: "t", aIndex: 0, bIndex: 0}
    1: {line: "h", aIndex: 1, bIndex: 1}
    2: {line: "e", aIndex: 2, bIndex: 2}
    3: {line: " ", aIndex: -1, bIndex: 3}
    4: {line: "l", aIndex: 3, bIndex: 4}
    5: {line: "e", aIndex: 4, bIndex: 5}
    6: {line: "b", aIndex: 5, bIndex: 6}
    7: {line: "r", aIndex: 6, bIndex: 7}
    8: {line: "o", aIndex: 7, bIndex: 8}
    9: {line: "n", aIndex: 8, bIndex: 9}
    10: {line: "n", aIndex: 9, bIndex: -1}
    11: {line: " ", aIndex: -1, bIndex: 10}
    12: {line: "j", aIndex: 10, bIndex: 11}
    13: {line: "a", aIndex: 11, bIndex: 12}
    14: {line: "m", aIndex: 12, bIndex: 13}
    15: {line: "i", aIndex: 13, bIndex: -1}
    16: {line: "e", aIndex: -1, bIndex: 14}
    17: {line: "s", aIndex: 14, bIndex: 15}
    18: {line: "t", aIndex: 15, bIndex: -1}
    
    0 ở vị trí 13.
  • Phần tử 16 chỉ ra rằng ký tự "E" đã được tìm thấy trong b ở vị trí 14.
  • Phần tử 18 chỉ ra rằng ký tự "T" đã được tìm thấy trong
    difference.lines: Array(19)
    
    0: {line: "t", aIndex: 0, bIndex: 0}
    1: {line: "h", aIndex: 1, bIndex: 1}
    2: {line: "e", aIndex: 2, bIndex: 2}
    3: {line: " ", aIndex: -1, bIndex: 3}
    4: {line: "l", aIndex: 3, bIndex: 4}
    5: {line: "e", aIndex: 4, bIndex: 5}
    6: {line: "b", aIndex: 5, bIndex: 6}
    7: {line: "r", aIndex: 6, bIndex: 7}
    8: {line: "o", aIndex: 7, bIndex: 8}
    9: {line: "n", aIndex: 8, bIndex: 9}
    10: {line: "n", aIndex: 9, bIndex: -1}
    11: {line: " ", aIndex: -1, bIndex: 10}
    12: {line: "j", aIndex: 10, bIndex: 11}
    13: {line: "a", aIndex: 11, bIndex: 12}
    14: {line: "m", aIndex: 12, bIndex: 13}
    15: {line: "i", aIndex: 13, bIndex: -1}
    16: {line: "e", aIndex: -1, bIndex: 14}
    17: {line: "s", aIndex: 14, bIndex: 15}
    18: {line: "t", aIndex: 15, bIndex: -1}
    
    0 ở vị trí 15.

Lưu ý rằng thuật toán patienceiff rất hữu ích để so sánh hai khối văn bản hoặc chuỗi tương tự. Nó sẽ không cho bạn biết nếu các chỉnh sửa cơ bản đã xảy ra. Ví dụ, sau đây ...

let a = "james lebron";
let b = "lebron james";

let difference = patienceDiff( a.split(""), b.split("") );

... Trả về difference.lines chứa ...

difference.lines: Array(18)

0: {line: "j", aIndex: 0, bIndex: -1}
1: {line: "a", aIndex: 1, bIndex: -1}
2: {line: "m", aIndex: 2, bIndex: -1}
3: {line: "e", aIndex: 3, bIndex: -1}
4: {line: "s", aIndex: 4, bIndex: -1}
5: {line: " ", aIndex: 5, bIndex: -1}
6: {line: "l", aIndex: 6, bIndex: 0}
7: {line: "e", aIndex: 7, bIndex: 1}
8: {line: "b", aIndex: 8, bIndex: 2}
9: {line: "r", aIndex: 9, bIndex: 3}
10: {line: "o", aIndex: 10, bIndex: 4}
11: {line: "n", aIndex: 11, bIndex: 5}
12: {line: " ", aIndex: -1, bIndex: 6}
13: {line: "j", aIndex: -1, bIndex: 7}
14: {line: "a", aIndex: -1, bIndex: 8}
15: {line: "m", aIndex: -1, bIndex: 9}
16: {line: "e", aIndex: -1, bIndex: 10}
17: {line: "s", aIndex: -1, bIndex: 11}

Lưu ý rằng patienceiff không báo cáo hoán đổi tên đầu tiên và tên cuối cùng, mà là cung cấp kết quả cho thấy những nhân vật đã bị xóa khỏi

difference.lines: Array(19)

0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
0 và những ký tự nào được thêm vào b để kết thúc với kết quả của b.

EDIT: Đã thêm thuật toán mới được mệnh danh là patienceiffplus.

Sau khi nghiền ngẫm trong ví dụ cuối cùng được cung cấp ở trên cho thấy giới hạn của người yêu thích trong việc xác định các dòng có khả năng di chuyển, nó đã nhận ra rằng có một cách sử dụng thuật toán patienceiff để xác định xem bất kỳ dòng nào có thực sự có khả năng di chuyển thay vì chỉ hiển thị Xóa và bổ sung.

Nói tóm lại, tôi đã thêm thuật toán

difference.lines: Array(19)

0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
9 (vào repo github được xác định ở trên) vào cuối tệp patienceiff.js. Thuật toán
difference.lines: Array(19)

0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
9 lấy Alines đã bị xóa [] và thêm các Blines [] từ thuật toán
let a = "james lebron";
let b = "lebron james";

let difference = patienceDiff( a.split(""), b.split("") );
1 ban đầu và chạy chúng qua thuật toán
let a = "james lebron";
let b = "lebron james";

let difference = patienceDiff( a.split(""), b.split("") );
1 một lần nữa. IE,
difference.lines: Array(19)

0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
9 đang tìm kiếm các chuỗi chung dài nhất của các dòng có khả năng di chuyển, trong đó nó ghi lại điều này trong kết quả
let a = "james lebron";
let b = "lebron james";

let difference = patienceDiff( a.split(""), b.split("") );
1 ban đầu. Thuật toán
difference.lines: Array(19)

0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
9 tiếp tục điều này cho đến khi không tìm thấy dòng di chuyển nào nữa.

Bây giờ, sử dụng

difference.lines: Array(19)

0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
9, so sánh sau đây ...

let a = "james lebron";
let b = "lebron james";

let difference = patienceDiffPlus( a.split(""), b.split("") );

... Trả về difference.lines chứa ...

difference.lines: Array(18)

0: {line: "j", aIndex: 0, bIndex: -1, moved: true}
1: {line: "a", aIndex: 1, bIndex: -1, moved: true}
2: {line: "m", aIndex: 2, bIndex: -1, moved: true}
3: {line: "e", aIndex: 3, bIndex: -1, moved: true}
4: {line: "s", aIndex: 4, bIndex: -1, moved: true}
5: {line: " ", aIndex: 5, bIndex: -1, moved: true}
6: {line: "l", aIndex: 6, bIndex: 0}
7: {line: "e", aIndex: 7, bIndex: 1}
8: {line: "b", aIndex: 8, bIndex: 2}
9: {line: "r", aIndex: 9, bIndex: 3}
10: {line: "o", aIndex: 10, bIndex: 4}
11: {line: "n", aIndex: 11, bIndex: 5}
12: {line: " ", aIndex: 5, bIndex: 6, moved: true}
13: {line: "j", aIndex: 0, bIndex: 7, moved: true}
14: {line: "a", aIndex: 1, bIndex: 8, moved: true}
15: {line: "m", aIndex: 2, bIndex: 9, moved: true}
16: {line: "e", aIndex: 3, bIndex: 10, moved: true}
17: {line: "s", aIndex: 4, bIndex: 11, moved: true}

Lưu ý rằng patienceiff không báo cáo hoán đổi tên đầu tiên và tên cuối cùng, mà là cung cấp kết quả cho thấy những nhân vật đã bị xóa khỏi

difference.lines: Array(19)

0: {line: "t", aIndex: 0, bIndex: 0}
1: {line: "h", aIndex: 1, bIndex: 1}
2: {line: "e", aIndex: 2, bIndex: 2}
3: {line: " ", aIndex: -1, bIndex: 3}
4: {line: "l", aIndex: 3, bIndex: 4}
5: {line: "e", aIndex: 4, bIndex: 5}
6: {line: "b", aIndex: 5, bIndex: 6}
7: {line: "r", aIndex: 6, bIndex: 7}
8: {line: "o", aIndex: 7, bIndex: 8}
9: {line: "n", aIndex: 8, bIndex: 9}
10: {line: "n", aIndex: 9, bIndex: -1}
11: {line: " ", aIndex: -1, bIndex: 10}
12: {line: "j", aIndex: 10, bIndex: 11}
13: {line: "a", aIndex: 11, bIndex: 12}
14: {line: "m", aIndex: 12, bIndex: 13}
15: {line: "i", aIndex: 13, bIndex: -1}
16: {line: "e", aIndex: -1, bIndex: 14}
17: {line: "s", aIndex: 14, bIndex: 15}
18: {line: "t", aIndex: 15, bIndex: -1}
0 và những ký tự nào được thêm vào b để kết thúc với kết quả của b.

Làm thế nào để bạn so sánh hai chuỗi trong JavaScript?

Để so sánh các chuỗi trong javascript, bạn có thể sử dụng toán tử bình đẳng nghiêm ngặt (===), thuộc tính độ dài và phương pháp localCompare (), trong đó toán tử bình đẳng nghiêm ngặt so sánh các chuỗi dựa trên các giá trị của chúng, thuộc tính độ dài kết hợp với các toán tử so sánh so sánh các chuỗi Dựa trên độ dài của chúng (số lượng ký tự ...use the Strict Equality operator (===), length property, and localCompare() method, where the Strict Equality Operator compare strings based on their values, the length property in combination with Comparison operators compare strings based on their length (number of characters ...

Làm thế nào để bạn tìm thấy sự khác biệt giữa hai chuỗi?

chênh lệch () trả về sự khác biệt giữa hai chuỗi, trả về phần của chuỗi thứ hai, bắt đầu khác với lần thứ nhất.StringUtils.indexOfDiference () trả về chỉ mục mà tại đó chuỗi thứ hai bắt đầu phân kỳ từ chuỗi thứ nhất. returns the difference between two strings, returning the portion of the second string, which starts to differ from the first. StringUtils. indexOfDifference() returns the index at which the second string starts to diverge from the first.

Làm thế nào để bạn tìm thấy sự khác biệt giữa hai chuỗi trong TypeScript?

Sử dụng toán tử bình đẳng nghiêm ngặt (===) để kiểm tra xem hai chuỗi có bằng nhau trong TypeScript không, ví dụ:if (str1 === str2) {}.Toán tử bình đẳng nghiêm ngặt trả về true nếu các chuỗi bằng nhau, nếu không thì sai được trả về., e.g. if (str1 === str2) {} . The strict equality operator returns true if the strings are equal, otherwise false is returned.

Làm thế nào để bạn so sánh các ký tự trong hai chuỗi?

Sử dụng phương thức chuỗi.equals () so sánh hai chuỗi dựa trên chuỗi các ký tự có trong cả hai chuỗi.Phương pháp này được gọi là sử dụng hai chuỗi và trả về giá trị boolean.Nếu tất cả các ký tự của cả hai chuỗi đều bằng nhau thì phương thức trả về đúng khác sẽ được trả về. equals() method compares two strings based on the sequence of characters present in both the strings. The method is called using two strings and returns a boolean value. If all the characters of both the strings are equal then the method returns true else false gets returned.