Hướng dẫn how do you check if a string ends with another string javascript? - làm thế nào để bạn kiểm tra xem một chuỗi có kết thúc bằng một chuỗi javascript khác hay không?

Ví dụ

Kiểm tra xem một chuỗi kết thúc bằng "Thế giới":

Show

hãy để văn bản = "Hello World"; Đặt kết quả = text.endswith ("thế giới");
let result = text.endsWith("world");

Hãy tự mình thử »

hãy để văn bản = "Hello World"; Đặt kết quả = text.endswith ("thế giới");
let result = text.endsWith("world");

Hãy tự mình thử »

hãy để văn bản = "Hello World"; Đặt kết quả = text.endswith ("thế giới");


Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng

Phương thức

confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
0 trả về
confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
1 nếu một chuỗi kết thúc bằng một chuỗi được chỉ định.

Nếu không, nó trả về

confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
2.

Phương pháp

confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
0 là trường hợp nhạy cảm.


Xem thêm Phương pháp confirmEnding("Bastian", "n") should return true. confirmEnding("Connor", "n") should return false. confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false. largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000]. confirmEnding("He has to give me a new name", "name")should return true. confirmEnding("Open sesame", "same") should return true. confirmEnding("Open sesame", "pen") should return false. confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false. Do not use the built-in method .endsWith() to solve the challenge.4.

Cú pháp

String.endswith (searchValue, length)

Thông sốTham số
Sự mô tảSearchValue
The string to search for.
Yêu cầu. Chuỗi để tìm kiếm. chiều dài
The length of the string to search.
Default value is the length of the string.

Không bắt buộc. Độ dài của chuỗi để tìm kiếm. Giá trị mặc định là độ dài của chuỗi.

Giá trị trả vềTham số
Sự mô tảSearchValue


Yêu cầu. Chuỗi để tìm kiếm.

chiều dài

Không bắt buộc. Độ dài của chuỗi để tìm kiếm. Giá trị mặc định là độ dài của chuỗi.
text.endsWith("world", 11);

Hãy tự mình thử »


hãy để văn bản = "Hello World"; Đặt kết quả = text.endswith ("thế giới");

Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng

Phương thức
confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
0 trả về
confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
1 nếu một chuỗi kết thúc bằng một chuỗi được chỉ định.
Nếu không, nó trả về
confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
2.
Phương pháp
confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
0 là trường hợp nhạy cảm.
Xem thêm Phương pháp
confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
4.
Cú pháp
String.endswith (searchValue, length)String.endswith (searchValue, length)String.endswith (searchValue, length)String.endswith (searchValue, length)String.endswith (searchValue, length)

Thông số



Hướng dẫn how do you check if a string ends with another string javascript? - làm thế nào để bạn kiểm tra xem một chuỗi có kết thúc bằng một chuỗi javascript khác hay không?

Trong bài viết này, tôi sẽ giải thích cách giải quyết Freecodecamp trong thời gian xác nhận thử thách kết thúc. Điều này liên quan đến việc kiểm tra xem một chuỗi kết thúc với chuỗi các ký tự cụ thể.

Có hai cách tiếp cận mà tôi bao gồm:

  1. Sử dụng phương thức Subr ()
  2. sử dụng phương thức endswith ()

Mô tả thử thách thuật toán

Kiểm tra xem một chuỗi (đối số thứ nhất,
confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.
9) kết thúc bằng chuỗi mục tiêu đã cho (đối số thứ hai,
confirmEnding("Open sesame", "same")
0).

Thử thách này có thể được giải quyết bằng phương pháp

confirmEnding("Open sesame", "same")
1, được giới thiệu trong ES2015. Nhưng với mục đích của thử thách này, chúng tôi muốn bạn sử dụng một trong các phương pháp cơ sở JavaScript thay thế.

function confirmEnding(string, target) {
  return string;
}
confirmEnding("Bastian", "n");

Cung cấp các trường hợp thử nghiệm

confirmEnding("Bastian", "n") should return true.

confirmEnding("Connor", "n") should return false.

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.

confirmEnding("Open sesame", "pen") should return false.

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.

Do not use the built-in method .endsWith() to solve the challenge.

Cách tiếp cận số 1: Xác nhận kết thúc của một chuỗi có hàm tích hợp-với Subrtr ()

Đối với giải pháp này, bạn sẽ sử dụng phương thức String.prototype.substr ():

  • Phương thức
    confirmEnding("Open sesame", "same")
    2 trả về các ký tự trong một chuỗi bắt đầu tại vị trí được chỉ định thông qua số lượng ký tự được chỉ định.

Tại sao bạn sử dụng

confirmEnding("Open sesame", "same")
3?

Nếu Target.length là âm, phương thức Subr () sẽ bắt đầu đếm từ cuối chuỗi, đó là những gì bạn muốn trong thử thách mã này.

Bạn không muốn sử dụng

confirmEnding("Open sesame", "same")
4 để có được phần tử cuối cùng của chuỗi, bởi vì nếu mục tiêu dài hơn một chữ cái:

confirmEnding("Open sesame", "same")

Mục tiêu đã giành chiến thắng trở lại.

Vì vậy, ở đây

confirmEnding("Open sesame", "same")
3 sẽ nhận được chỉ số cuối cùng của chuỗi ‘Bastian, đó là’ n.

Sau đó, bạn kiểm tra xem

confirmEnding("Open sesame", "same")
3 bằng mục tiêu (đúng hay sai).


function confirmEnding(string, target) {
  // Step 1. Use the substr method
  if (string.substr(-target.length) === target) {
  
  // What does "if (string.substr(-target.length) === target)" represents?
  // The string is 'Bastian' and the target is 'n' 
  // target.length = 1 so -target.length = -1
  // if ('Bastian'.substr(-1) === 'n')
  // if ('n' === 'n')
  
  // Step 2. Return a boolean (true or false)
    return true;
  } else {
    return false;
  }
}

confirmEnding('Bastian', 'n');

Không có bình luận:


function confirmEnding(string, target) {
  if (string.substr(-target.length) === target) {
    return true;
  } else {
    return false;
  }
}
confirmEnding('Bastian', 'n');

Bạn có thể sử dụng toán tử ternary làm phím tắt cho câu lệnh IF:ternary operator as a shortcut for the if statement:

(string.substr(-target.length) === target) ? true : false;

Điều này có thể được đọc như:

if (string.substr(-target.length) === target) {
    return true;
} else {
    return false;
}

Sau đó, bạn trả lại toán tử ternary trong chức năng của bạn:


function confirmEnding(string, target) {
  return (string.substr(-target.length) === target) ? true : false;
}
confirmEnding('Bastian', 'n');

Bạn cũng có thể tái cấu trúc mã của mình để làm cho nó cô đọng hơn bằng cách trả lại điều kiện:

function confirmEnding(string, target) {
  return string.substr(-target.length) === target;
}
confirmEnding('Bastian', 'n');

Cách tiếp cận #2: Xác nhận kết thúc của một chuỗi có hàm tích hợp-với EndSwith ()

Đối với giải pháp này, bạn sẽ sử dụng phương thức String.prototype.endswith ():

  • Phương thức
    confirmEnding("Bastian", "n") should return true.
    
    confirmEnding("Connor", "n") should return false.
    
    confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.
    
    largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].
    
    confirmEnding("He has to give me a new name", "name")should return true.
    confirmEnding("Open sesame", "same") should return true.
    
    confirmEnding("Open sesame", "pen") should return false.
    
    confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.
    
    Do not use the built-in method .endsWith() to solve the challenge.
    0 xác định xem một chuỗi kết thúc với các ký tự của một chuỗi khác, trả về
    confirmEnding("Bastian", "n") should return true.
    
    confirmEnding("Connor", "n") should return false.
    
    confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.
    
    largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].
    
    confirmEnding("He has to give me a new name", "name")should return true.
    confirmEnding("Open sesame", "same") should return true.
    
    confirmEnding("Open sesame", "pen") should return false.
    
    confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.
    
    Do not use the built-in method .endsWith() to solve the challenge.
    1 hay
    confirmEnding("Bastian", "n") should return true.
    
    confirmEnding("Connor", "n") should return false.
    
    confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.
    
    largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].
    
    confirmEnding("He has to give me a new name", "name")should return true.
    confirmEnding("Open sesame", "same") should return true.
    
    confirmEnding("Open sesame", "pen") should return false.
    
    confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.
    
    Do not use the built-in method .endsWith() to solve the challenge.
    2 nếu thích hợp. Phương pháp này nhạy cảm trường hợp.
function confirmEnding(string, target) {
  // We return the method with the target as a parameter
  // The result will be a boolean (true/false)
  return string.endsWith(target); // 'Bastian'.endsWith('n')
}
confirmEnding('Bastian', 'n');

Tôi hy vọng bạn tìm thấy điều này hữu ích. Đây là một phần trong loạt bài viết của tôi Cách giải quyết các bài viết của FCC Thuật toán về các thách thức thuật toán Freecodecamp, trong đó tôi đề xuất một số giải pháp và giải thích từng bước những gì xảy ra dưới mui xe.

Ba cách để lặp lại một chuỗi trong JavaScriptin Bài viết này, tôi sẽ giải thích cách giải quyết Freecodecamp, lặp lại một chuỗi lặp lại một thử thách chuỗi. Điều này liên quan đến
In this article, I’ll explain how to solve freeCodeCamp’s “Repeat a string repeat a string” challenge. This involves…

Ba cách để đảo ngược một chuỗi trong bài viết của JavaScriptthis dựa trên thuật toán cơ bản của Code Code miễn phí
This article is based on Free Code Camp Basic Algorithm Scripting “Reverse a String”

Ba cách để nhân viên hóa một số trong bài viết của JavaScriptthis dựa trên thuật toán cơ bản của Code Camp miễn phí
This article is based on Free Code Camp Basic Algorithm Scripting “Factorialize a Number”

Hai cách để kiểm tra palindromes trong bài viết của JavaScriptthis dựa trên thuật toán cơ bản của Code Camp miễn phí Kiểm tra palindromes.
This article is based on Free Code Camp Basic Algorithm Scripting “Check for Palindromes”.

Ba cách để tìm từ dài nhất trong một chuỗi trong bài viết của JavaScriptthis dựa trên tập lệnh thuật toán cơ bản Camp Camp miễn phí Tìm từ dài nhất trong một chuỗi.
This article is based on Free Code Camp Basic Algorithm Scripting “Find the Longest Word in a String”.

Ba cách để tiêu đề trường hợp một câu trong bài viết của JavaScriptthis dựa trên thuật toán cơ bản Camp Camp miễn phí Trường hợp tiêu đề của một câu.
This article is based on Free Code Camp Basic Algorithm Scripting “Title Case a Sentence”.

Nếu bạn có giải pháp riêng hoặc bất kỳ đề xuất nào, hãy chia sẻ chúng dưới đây trong các ý kiến.

Hoặc bạn có thể theo dõi tôi trên Medium, Twitter, GitHub và LinkedIn, ngay sau khi bạn nhấp vào trái tim xanh bên dưới ;-)Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-)

#Ở lại, #Keeponhacking & #MakeithAppen!

Tài nguyên bổ sung

  • Phương thức Subr () - MDN
  • Phương thức EndSwith () - MDN
  • Nhà điều hành Ternary - MDN


Học mã miễn phí. Chương trình giảng dạy nguồn mở của 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

Làm thế nào để bạn kiểm tra xem một chuỗi kết thúc bằng một chuỗi khác?

Phương thức endSwith () trả về true nếu một chuỗi kết thúc bằng một chuỗi được chỉ định.Nếu không thì nó trả về sai.Phương thức endswith () là trường hợp nhạy cảm. returns true if a string ends with a specified string. Otherwise it returns false . The endsWith() method is case sensitive.

Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một chuỗi khác trong javascript không?

Phương thức bao gồm () trả về true nếu một chuỗi chứa một chuỗi được chỉ định.Nếu không thì nó trả về sai.Phương pháp bao gồm () là trường hợp nhạy cảm. returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.

Làm thế nào để bạn xác định kết thúc của một chuỗi?

Phương thức EndSwith () xác định xem một chuỗi kết thúc bằng các ký tự của chuỗi khác, trả về đúng hay sai khi thích hợp. determines whether a string ends with the characters of another string, returning true or false as appropriate.

Làm thế nào để bạn kiểm tra xem một chuỗi kết thúc với một số trong javascript?

Để kiểm tra xem một chuỗi kết thúc bằng một số, hãy gọi phương thức kiểm tra () trên biểu thức thông thường khớp với một hoặc nhiều số ở cuối chuỗi.Phương thức kiểm tra trả về true nếu biểu thức chính quy được khớp trong chuỗi và sai.call the test() method on a regular expression that matches one or more numbers at the end a string. The test method returns true if the regular expression is matched in the string and false otherwise.