您好,欢迎访问一九零五行业门户网

如何在 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 firststring 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 firststring 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 目标长度
填充后最终字符串所需的长度。
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() 并给它一个字符串,“是在线教程”,我们给了 paddsecondstr结果。
该过程通过附加“是在线教程”来扩展“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() 中使用 long padstring。
在下面的示例中,“带有教程点的 javascript”已作为 padstring 传递。通过 padend() 方法对指定的 padstring 进行修剪,使填充后的字符串长度等于 targetlength (21)。
因此,firststring.padend(21, javascript withtutorialspoint) 返回长度为 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>
以上就是如何在 javascript 中连接两个字符串,以便第二个字符串必须连接在第一个字符串的末尾?的详细内容。
其它类似信息

推荐信息