Trang chủ / Javascript / Lấy ký tự đầu chuỗi thêm vào cuối chuỗi

Lấy ký tự đầu chuỗi thêm vào cuối chuỗi

Viết chương trình JavaScript để tạo một chuỗi mới từ một chuỗi đã cho với ký tự đầu tiên của chuỗi đã cho được thêm vào ở phía trước và phía sau.

Ví dụ:

JavaScript: Create a new string from a given string with the first character of the given string added at the front and back

Mã nguồn:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JavaScript program to create a new string from a given string with the first character of the given string added at the front and back.</title>
 <script>
function front_back(str)
{
  first = str.substring(0,1);
  return first + str + first;
}
document.write(front_back('a'));
document.write(front_back('ab'));
document.write(front_back('abc'));
 </script>
</head>
<body>

</body>
</html>

Xem ví dụ

 

Lưu đồ thuật toán:

Flowchart: JavaScript - Create a new string from a given string with the first character of the given string added at the front and back

Thấy bài viết hữu ích?

Ủng hộ tác giả để có thêm nhiều bài viết chất lượng nhé!

Tư vấn lộ trình CNTT 🤖