iT邦幫忙

2024 iThome 鐵人賽

DAY 15
0
自我挑戰組

使用 Microsoft Copilot 製作衛教網站系列 第 15

第15天:學習 JavaScript 中的資料類型:字串 (string)

  • 分享至 

  • xImage
  •  

第15天:學習 JavaScript 中的資料類型:字串 (string)

今天是我參加 IT 鐵人賽的第十五天,我決定學習 JavaScript 中的字串資料類型。字串是程式設計中最常用的資料類型之一,掌握字串的基本操作對於處理文本數據和進行各種文本操作至關重要。

首先,我學習了 JavaScript 中字串資料類型的基本操作。字串可以用單引號(')、雙引號(")或反引號(`)來表示。常見的字串操作包括連接(concatenation)、取子字串(substring)、查找(search)和替換(replace)等。我還學習了如何使用字串方法,例如 length、toUpperCase() 和 toLowerCase() 來處理字串。

再來,我實作了一些範例來鞏固我的學習。我首先宣告了一些字串變數,並進行基本的字串操作。然後,我使用內建的字串方法來處理這些字串,並觀察它們的輸出結果。

範例如下:
// 宣告字串變數
let greeting = "Hello, World!";
let name = 'Alice';
let message = Welcome to the IT Ironman competition, ${name}!;

// 字串連接
let fullGreeting = greeting + " " + message;
console.log(fullGreeting); // 輸出: Hello, World! Welcome to the IT Ironman competition, Alice!

// 取子字串
let substring = greeting.substring(0, 5);
console.log(substring); // 輸出: Hello

// 查找字串
let index = greeting.indexOf("World");
console.log(index); // 輸出: 7

// 替換字串
let newGreeting = greeting.replace("World", "JavaScript");
console.log(newGreeting); // 輸出: Hello, JavaScript!

// 字串方法
console.log(greeting.length); // 輸出: 13
console.log(greeting.toUpperCase()); // 輸出: HELLO, WORLD!
console.log(greeting.toLowerCase()); // 輸出: hello, world!

今天的學習讓我對 JavaScript 中的字串資料類型有了更深入的理解,並且能夠靈活運用基本的字串操作和字串方法來處理文本數據。我期待在接下來的日子裡,能夠學習更多關於 JavaScript 的知識,並將它們應用到我的衛教網站中。


上一篇
第14天:學習資料類型:數字 (number)
下一篇
第16天:學習JavaScript 中的資料類型:布林 (boolean) ​
系列文
使用 Microsoft Copilot 製作衛教網站16
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言