Javascript 打印由空格分隔的所有字符
我有一个变量,我想用一个空格字母一个字母地分隔名称,然后用大写字母分隔名称 var name=“汤姆·汉克斯” console.log(名称)必须等于“TOMHANKS”Javascript 打印由空格分隔的所有字符,javascript,Javascript,我有一个变量,我想用一个空格字母一个字母地分隔名称,然后用大写字母分隔名称 var name=“汤姆·汉克斯” console.log(名称)必须等于“TOMHANKS” var name=“汤姆·汉克斯”; var result=name.toUpperCase().split(“”)。join(“”)。replace(/\s+/g,“”); 控制台日志(结果)首先必须拆分字符串以分离单词的字母,并将它们保存在数组对象中。为此,您可以使用String.split()函数: const mySt
var name=“汤姆·汉克斯”;
var result=name.toUpperCase().split(“”)。join(“”)。replace(/\s+/g,“”);
控制台日志(结果)代码>首先必须拆分字符串以分离单词的字母,并将它们保存在数组对象中。为此,您可以使用String.split()
函数:
const myString = 'Tom Hanks';
const splittedString = myString.split('');
然后,您可以使用Array.join()
函数创建一个新字符串,在上一个数组的字母之间留有空格:
const stringWithSpaces = splittedString.join(' ');
最后,您可以使用String.toUpperCase()
设置“大写锁定”:
下面是完整的片段:
const myString = 'Tom Hanks';
const splittedString = myString.split('');
const stringWithSpaces = splittedString.join(' ');
const upperCaseStringWithSpaces = stringWithSpaces.toUpperCase();
console.log(upperCaseStringWithSpaces);
欢迎来到StackOverflow。请检查一下电话号码。您还可以在上找到大量有用的Javascript文档。您可能需要.split()
和.toUpperCase()
欢迎使用SO!作为一个新用户,我建议大家复习一下形成问题的技巧。一般来说,希望您展示您迄今为止所做的尝试,并说明您面临的具体问题,以便社区帮助您。当你简单地陈述你的需求时,它创造了一个你试图依赖的外观,作为一个自由软件写作平台,或者作为一个家庭作业服务。谢谢!它在没有“.replace(/\s+/g,”)的情况下工作
const myString = 'Tom Hanks';
const splittedString = myString.split('');
const stringWithSpaces = splittedString.join(' ');
const upperCaseStringWithSpaces = stringWithSpaces.toUpperCase();
console.log(upperCaseStringWithSpaces);