如何在 JavaScript 中連線兩個字串,使第二個字串必須連線到第一個字串的末尾?
concat() 方法
組合兩個字串的基本操作是連線。字串組合是程式設計的必要部分。在討論“JavaScript 中的字串連線”之前,我們需要首先弄清楚基本原理。當直譯器執行操作時,會生成一個新的字串。
為了建立一個新字串,concat() 方法將呼叫字串和字串引數連線起來。初始字串和返回的字串都不會受到任何一方更改的影響。
連線時,字串值會首先從不是字串型別的引數轉換而來。
語法
以下是 concat() 方法的語法
concat(str1) concat(str1, str2) concat(str1, str2, ... , strN)
| 序號 | 引數及說明 |
|---|---|
| 1 |
strN 使用一個或多個字串進行字串連線 |
返回值
concat() 方法透過連線初始字串和作為引數傳遞的字串值來生成一個新字串。
示例 1
此方法返回一個組合兩個輸入字串的新字串;它不會以任何方式更改給定或輸入字串。給定引數必須絕對是字串,這是 concat() 方法的一個缺點。
此方法接受非字串引數,但是如果給定字串等於 null,則會丟擲 TypeError。此外,由於 JavaScript 中的字串是不可變的,因此 concat() 方法實際上不會更改字串。
<!DOCTYPE html> <html> <title>How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <div id="result"></div> <body> <script> let string1 = "Tutorialspoint is the best "; let string2 = "website available "; let string3 = "for online education."; let concatRes = string1.concat(string2, string3); document.getElementById("result").innerHTML =concatRes; </script> </body> </html>
示例 2
在本例中,讓我們瞭解使用空字串變數進行連線。我們將使用 concat() 方法和一個空字串變數來連線簡單的字串值。
<!DOCTYPE html> <html> <title>How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <div id="result"></div> <body> <script> let tutpoint_string = ''; document.getElementById("result").innerHTML =tutpoint_string.concat('Visit ','Tut','orials','point!'); </script> </body> </html>
示例 3
為了新增兩個數字,我們使用 + 運算子。JavaScript 也支援字串連線。+ 運算子在字串組合時具有獨特的屬性。您可以追加到現有字串以更改它,或者您也可以僅用於建立新字串。
<!DOCTYPE html> <html> <title>How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <div id="result"></div> <body> <script> let firstStr = 'Tutorials Point is a '; let secondStr = ' '; let thirdStr = 'leading Ed Tech company'; document.getElementById("result").innerHTML =firstStr+secondStr+thirdStr; </script> </body> </html>
string.padEnd() 方法
可以使用 string.concat() 方法進行連線。但是,它不會提供任何資訊,例如連線後字串的長度或必須新增的確切數字等。
因此,Javascript 提供了 string.padEnd() 方法來幫助解決此問題。此過程需要兩個引數。第一個是長度,第二個是要附加到初始字串的附加字串。
使用 padEnd() 函式用一些其他字串填充字串。為了使字串填充到達到特定長度,它會執行此操作。
填充應用於字串的右側末尾。因此,它也稱為右填充。
語法
padEnd(targetLength) padEnd(targetLength, padString)
| 序號 | 引數及說明 |
|---|---|
| 1 | targetLength 填充後最終字串所需的長度。 |
| 2 | pad_string 可選。提供的字串是將在字串末尾填充的內容。如果忽略此選項,則 padEnd() 方法將用空格替換填充字元。 |
返回值
padEnd 方法的返回值是一個字串,該字串已在結尾處透過給定字串加長。
示例 4
在本例中,讓我們瞭解如何使用 padEnd() 方法。我們為 firstString 提供了字串值“TutorialsPoint”,並使用 padEnd() 方法在其末尾添加了一個“$”符號。此外,我們在方法內部將 20 作為 targetLength。
因此,該方法返回 20 個字元長的最終字串“Tutorialspoint$$$$$$”。
<!DOCTYPE html> <html> <title>How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <div id="result"></div> <body> <script> let firstString = "Tutorialspoint"; let paddfirstStr= firstString.padEnd(20, "$"); document.getElementById("result").innerHTML =paddfirstStr; </script> </body> </html>
示例 5
在本例中,讓我們瞭解如何在 padEnd() 中使用多個字元 padString。在下面的示例中,我們呼叫了 padEnd() 併為其提供了一個字元字串“is online tutorials”,並將結果賦給了 paddSecondStr。
該過程透過追加“is online tutorials”擴充套件“Tutorialspoint”,直到最終字串為 26 個字元長。paddSecondStr 返回的最終字串“Tutorialspointis online tu”,其長度為 26。
<!DOCTYPE html> <html> <title>How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <div id="result"></div> <body> <script> let firstString = "Tutorialspoint "; let paddSecondStr= firstString.padEnd(26, 'is online tutorials'); document.getElementById("result").innerHTML =paddSecondStr; </script> </body> </html>
示例 6
在本例中,讓我們瞭解如何在 padEnd() 中使用長 padString。
在下面的示例中,“JavaScript with tutorialspoint”已作為 padString 傳遞。padEnd() 方法會修剪指定的 padString,以便填充後字串的長度等於 targetLength (21)。
因此,最終字串“Learn JavaScript with”,長度為 21,由 firstString.padEnd(21, “JavaScript with tutorialspoint”) 返回。
<!DOCTYPE html> <html> <title>How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <div id="result"></div> <body> <script> let firstString = "Learn "; let paddThirdStr= firstString.padEnd(21, 'JavaScript with tutorialspoint'); document.getElementById("result").innerHTML =paddThirdStr; </script> </body> </html>
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP