Bạn có thể muốn so sánh các kết quả trả về của bao gồm [] với các toán hạng bằng nhau, === false hoặc === true, nó tốt hơn nhiều tuy nhiên không thực sự cần thiết cho điều này, có vẻ như bạn có thể được hưởng lợi từ việc biết khác nhau như So sánh Boolean với một chuỗi là một điều kỳ lạ để làm. Tôi cũng sẽ không kiểm tra "luồng" và "luồng" hãy thử sử dụng tolowercase [] thay vào đó như vậy, var str1_check = gewaesser_name1.toleowercase [];
Tôi sẽ kiểm tra luồng bằng cách sử dụng "luồng" viết thường vì tất cả các chuỗi mới của bạn sẽ ở trường hợp thường, cũng như bạn muốn chúng tách biệt với các biến ban đầu của bạn vì bạn có thể không muốn những tên đó bị buộc phải viết thường. Tôi sẽ sử dụng str1_check.includes ["stream"] để kiểm tra xem chuỗi này có chuỗi "luồng" trong đó không, bởi vì kết quả này là sự thật hay sai bạn có thể thực hiện kiểm tra của mình như vậy.
if[str1_check.includes["stream"]] {
//this string contained stream
}
Tôi trông giống như logic của bạn ở đây là nếu tên đầu tiên không chứa "luồng" hoặc tên 1 và 2 không chứa luồng nhưng tên kiểm tra của bạn 1 với chữ thường "luồng" và tên 2 với "luồng". Có vẻ như bạn chỉ muốn cả hai tên không chứa luồng, điều này có thể dễ dàng thực hiện hơn nhiều như thế này.
var str1_check = gewaesser_name1.toLowerCase[],
str2_check = gewaesser_name2.toLowrCase[];//this way you're not making multiple toLowerCase calls in your conditional and maintain the state of your two names.
if[!str1_check.includes["stream"] && !str2_check.includes["stream"]]{
//your code on truthey statement
}
Kiểm tra xem chuỗi không bao gồm chuỗi con #
Để kiểm tra xem một chuỗi không bao gồm một chuỗi con, hãy sử dụng toán tử logic không [!] Để phủ định cuộc gọi đến phương thức includes[]
. Toán tử không [!] Trả về false
khi được gọi trên giá trị true
và ngược lại.
Copied!
const str = 'hello world'; if [!str.includes['bye']] { console.log['✅ string does not include substring']; } else { console.log['⛔️ string includes substring']; }
Chúng tôi đã sử dụng toán tử logic không [!] Để phủ định cuộc gọi đến phương thức chuỗi.includes.
Trong ví dụ, chúng tôi kiểm tra xem giá trị bye
có chứa trong chuỗi
var str1_check = gewaesser_name1.toLowerCase[],
str2_check = gewaesser_name2.toLowrCase[];//this way you're not making multiple toLowerCase calls in your conditional and maintain the state of your two names.
if[!str1_check.includes["stream"] && !str2_check.includes["stream"]]{
//your code on truthey statement
}
0 không.Copied!
const str = 'hello world'; console.log[str.includes['bye']]; // 👉️ false console.log[!str.includes['bye']]; // 👉️ true
Nếu chúng ta sử dụng phương thức
var str1_check = gewaesser_name1.toLowerCase[],
str2_check = gewaesser_name2.toLowrCase[];//this way you're not making multiple toLowerCase calls in your conditional and maintain the state of your two names.
if[!str1_check.includes["stream"] && !str2_check.includes["stream"]]{
//your code on truthey statement
}
1 mà không có toán tử logic không [!], Nó sẽ trả về true
nếu giá trị được chứa trong chuỗi và false
khác.Bằng cách phủ định giá trị trả về từ phương thức
1, chúng tôi nhận được nếu giá trị không có trong chuỗi.var str1_check = gewaesser_name1.toLowerCase[],
str2_check = gewaesser_name2.toLowrCase[];//this way you're not making multiple toLowerCase calls in your conditional and maintain the state of your two names.
if[!str1_check.includes["stream"] && !str2_check.includes["stream"]]{
//your code on truthey statement
}
Dưới đây là một số ví dụ khác về việc sử dụng toán tử logic không [!].
Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Bạn có thể tưởng tượng rằng toán tử logic không [!] Trước tiên chuyển đổi giá trị thành
var str1_check = gewaesser_name1.toLowerCase[],
str2_check = gewaesser_name2.toLowrCase[];//this way you're not making multiple toLowerCase calls in your conditional and maintain the state of your two names.
if[!str1_check.includes["stream"] && !str2_check.includes["stream"]]{
//your code on truthey statement
}
5 và sau đó lật giá trị.Khi bạn phủ định giá trị giả, toán tử trả về true
, trong tất cả các trường hợp khác, nó sẽ trả về false
.
Các giá trị giả là:
var str1_check = gewaesser_name1.toLowerCase[],
str2_check = gewaesser_name2.toLowrCase[];//this way you're not making multiple toLowerCase calls in your conditional and maintain the state of your two names.
if[!str1_check.includes["stream"] && !str2_check.includes["stream"]]{
//your code on truthey statement
}
8, var str1_check = gewaesser_name1.toLowerCase[],
str2_check = gewaesser_name2.toLowrCase[];//this way you're not making multiple toLowerCase calls in your conditional and maintain the state of your two names.
if[!str1_check.includes["stream"] && !str2_check.includes["stream"]]{
//your code on truthey statement
}
9, chuỗi trống, 0,Copied!
const str = 'hello world'; if [!str.includes['bye']] { console.log['✅ string does not include substring']; } else { console.log['⛔️ string includes substring']; }
1 vàCopied!
const str = 'hello world'; if [!str.includes['bye']] { console.log['✅ string does not include substring']; } else { console.log['⛔️ string includes substring']; }
false
.Phương pháp Copied! Copied!
3 không được hỗ trợ trong Internet Explorer. Nếu bạn phải hỗ trợ trình duyệt, hãy sử dụng phương thức const str = 'hello world';
if [!str.includes['bye']] {
console.log['✅ string does not include substring'];
} else {
console.log['⛔️ string includes substring'];
}
4 thay thế.const str = 'hello world';
if [!str.includes['bye']] {
console.log['✅ string does not include substring'];
} else {
console.log['⛔️ string includes substring'];
}
Kiểm tra xem chuỗi không bao gồm chuỗi con với indexof #
Để kiểm tra xem một chuỗi không bao gồm một chuỗi con, hãy gọi đến phương thức Copied! Copied! Copied!
5 trên chuỗi, chuyển nó dưới dạng phụ dưới dạng tham số. Nếu phương thức const str = 'hello world';
if [!str.includes['bye']] {
console.log['✅ string does not include substring'];
} else {
console.log['⛔️ string includes substring'];
}
4 trả về const str = 'hello world';
if [!str.includes['bye']] {
console.log['✅ string does not include substring'];
} else {
console.log['⛔️ string includes substring'];
}
7, thì chuỗi con không được chứa trong chuỗi.const str = 'hello world';
if [!str.includes['bye']] {
console.log['✅ string does not include substring'];
} else {
console.log['⛔️ string includes substring'];
}
Copied!
// Supported in IE const str = 'hello world'; if [str.indexOf['bye'] === -1] { console.log['✅ string does not include substring']; } else { console.log['⛔️ string includes substring']; }
Phương thức chuỗi.Indexof trả về chỉ mục của lần xuất hiện đầu tiên của một giá trị trong một chuỗi. Nếu giá trị không chứa trong chuỗi, phương thức trả về
7.Copied!
const str = 'hello world'; if [!str.includes['bye']] { console.log['✅ string does not include substring']; } else { console.log['⛔️ string includes substring']; }
Nếu điều kiện được đáp ứng và khối
9 chạy, chúng tôi có thể chắc chắn rằng giá trị được cung cấp không được chứa trong chuỗi.Copied!
const str = 'hello world'; if [!str.includes['bye']] { console.log['✅ string does not include substring']; } else { console.log['⛔️ string includes substring']; }
Đọc thêm #
- Kiểm tra xem thuộc tính đối tượng không được xác định trong JavaScript
- Kiểm tra xem chuỗi có phải là số nguyên dương trong javascript không