Javascript - Kiểm tra xem một chuỗi có kết thúc bằng hậu tố cho trước?
Viết một hàm JavaScript để kiểm tra xem một chuỗi có kết thúc bằng hậu tố cho trước?
Ví dụ:
Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to convert the letters of a given string in alphabetical order.</title>
</head>
<script>
function string_endsWith(str, suffix)
{
if (((str===null) || (str==='')) || ((suffix===null) || (suffix==='')))
{
return false;
}
else
{
str = str.toString();
suffix = suffix.toString();
}
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
document.write(string_endsWith('JS PHP PYTHON','PYTHON')+"<br/>");
document.write(string_endsWith('JS PHP PYTHON',''));
</script>
<body>
</body>
</html>
Lưu đồ thuật toán: