如何在 JavaScript 中将字符串剪切到 X 个字符后?
javascriptweb developmentobject oriented programming更新于 2025/10/30 2:20:17
要在 X 个字符后剪切字符串,请使用 JavaScript 中的 substr() 函数。以下是 JavaScript 代码,其中我们在第 9 个字符后剪切字符串 −
示例
var myName="JohnSmithMITUS";
console.log("The String="+myName)
var afterXCharacter = myName.substr(0, 9) + "\u0026";
console.log("After Cutting the characters the string="+afterXCharacter);
以上,unicode “\u0026” 将用 &(“\u0026”) 替换所有字符。
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里,文件名是 demo34.js。
输出
这将产生以下输出。
PS C:\Users\Amit\JavaScript-code> node demo34.js The String=JohnSmithMITUS After cutting the characters the string=JohnSmith&
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

