Hướng dẫn reverse a string in javascript using inbuilt function - đảo ngược một chuỗi trong javascript bằng chức năng sẵn có
Ngày đăng:
24/11/2022
Trả lời:
0
Lượt xem:
73
Ví dụ 1: Đảo ngược một chuỗi bằng cách sử dụng cho vòng lặp
Đầu ra Show Enter a string: hello world dlrow olleh Trong chương trình trên, người dùng được nhắc nhập một chuỗi. Chuỗi đó được chuyển đến hàm Bên trong hàm
Ví dụ 2: Đảo ngược chuỗi bằng cách sử dụng các phương thức tích hợp
Đầu ra Enter a string: hello olleh Trong chương trình trên, người dùng được nhắc nhập một chuỗi. Chuỗi đó được chuyển đến hàm
Phương pháp 3: & NBSP; Sử dụng toán tử lây lan thay vì chức năng chia () để chuyển đổi chuỗi thành mảng các ký tự, tức là ['g', 'e', 'e', 'k', 's', '', 'f', 'o', ',' r ',' ',' g ',' e ',' e ',' k ',' s ']. Tìm hiểu thêm về toán tử lây lan ở đây skeeG rof skeeG3 skeeG rof skeeG0 skeeG rof skeeG1Examples: Input: str = "Geeks for Geeks" Output: "skeeG rof skeeG" Input: str = "Hello" Output: "olleH" Có nhiều phương pháp để đảo ngược một chuỗi trong JavaScript Một số trong số chúng được thảo luận dưới đây: Phương pháp 1:
Example: JavaScriptEnter a string: hello world dlrow olleh9 0 1 2 3 4 5 6 7 8 9Enter a string: hello olleh0 Enter a string: hello olleh1 Enter a string: hello olleh2 Enter a string: hello olleh3 2Enter a string: hello olleh5 2Enter a string: hello olleh7 2Enter a string: hello olleh9 2Enter a string: hello world dlrow olleh1 Input: str = "Geeks for Geeks" Output: "skeeG rof skeeG" Input: str = "Hello" Output: "olleH"2 Enter a string: hello olleh0 Input: str = "Geeks for Geeks" Output: "skeeG rof skeeG" Input: str = "Hello" Output: "olleH"4 2Enter a string: hello olleh5 2Enter a string: hello olleh1 Input: str = "Geeks for Geeks" Output: "skeeG rof skeeG" Input: str = "Hello" Output: "olleH"9 skeeG rof skeeG0 skeeG rof skeeG1 Enter a string: hello olleh5 skeeG rof skeeG3 Enter a string: hello world dlrow olleh1 skeeG rof skeeG5 skeeG rof skeeG6 Output: skeeG rof skeeG Phương pháp 2:
Example: JavaScriptEnter a string: hello world dlrow olleh9 0 1 5 6 7 8 9Enter a string: hello olleh5 skeeG rof skeeG3 Enter a string: hello world dlrow olleh1 skeeG rof skeeG5 skeeG rof skeeG6 Output: skeeG rof skeeG Phương pháp 2:
Example: JavaScriptEnter a string: hello world dlrow olleh9 0 1skeeG rof skeeG3 Enter a string: hello world dlrow olleh1 skeeG rof skeeG5 skeeG rof skeeG6 Output: skeeG rof skeeG Method#4:
Example: Sử dụng hàm lart () trong javascript để đảo ngược mảng các ký tự, tức là ['s', 'k', 'e', 'e', 'g', '', 'r', 'o', 'f', '', 's', 'k', 'e', 'e', 'g']]]Enter a string: hello world dlrow olleh9 0 1 5 6 7 8 9Enter a string: hello olleh5 skeeG rof skeeG7 skeeG rof skeeG8 skeeG rof skeeG9 skeeG rof skeeG6 Output: skeeG rof skeeG |