如何在 JavaScript 中连接两个字符串,使得第二个字符串必须连接到第一个字符串的末尾?

front end technologyjavascriptobject oriented programming

concat() 方法

连接两个字符串的基本操作是连接。字符串组合是编程的必要部分。在讨论"JavaScript 中的字符串连接"之前,我们需要先理清基础知识。当解释器执行操作时,会产生一个新字符串。

为了创建新字符串,concat() 方法将调用字符串和字符串参数连接起来。初始字符串和返回的字符串不受任何更改的影响。

连接时,字符串值首先从非字符串类型的参数转换而来。

语法

以下是 concat() 方法的语法

concat(str1)
concat(str1, str2)
concat(str1, str2, ... , strN)
Sr.No
 参数 &描述
1
strN
一个或多个字符串的字符串连接

返回

concat() 方法通过连接初始字符串和作为参数传递的字符串值来生成一个新字符串。

示例

此方法返回一个结合两个输入字符串的新字符串;它不会以任何方式更改给定或输入的字符串。给定的参数必须是字符串,这是 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>

示例

在此示例中,让我们了解如何使用空字符串变量进行连接。我们将使用 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>

示例

为了将两个数字相加,我们使用 + 运算符。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)
Sr.No
参数 &描述
1
targetLength
填充后最终字符串的期望长度。
2
pad_string
可选。提供的字符串是将在字符串末尾填充的内容。如果忽略此选项,padEnd() 方法将用空格代替 pad 字符。

返回

padEnd 方法的返回值是一个字符串,该字符串的末尾已由给定的字符串加长。

示例

在示例中,让我们了解如何使用 padEnd() 方法。我们为 firstString 赋予字符串值"TutorialsPoint",并使用 padEnd() 方法在其末尾添加了"$"符号。此外,我们在方法中将 targetLength 设置为 20。

因此,该方法返回 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>

示例

在此示例中,让我们了解如何在 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>

示例

在此示例中,让我们了解如何在 padEnd() 中使用 Long padString。

在下面的示例中,"JavaScript with tutorialspoint"已作为 padString 传递。指定的 padString 由 padEnd() 方法修剪,以便填充后的字符串长度等于 targetLength (21)。

因此,firstString.padEnd(21, "JavaScript with tutorialspoint") 返回长度为 21 的最终字符串"Learn JavaScript with"。

<!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>

相关文章