[英]Extract unique characters from a string with keeping whitespace between strings
How do I extract unique letter occurrences in a string, including all spaces? 如何在字符串中提取唯一字母出现,包括所有空格?
The problem with my code is that it only returns the first occurrence of the space: 我的代码的问题是它只返回第一次出现的空格:
function unique_char(str1) {
var str = str1;
var uniql = "";
for (var x = 0; x < str.length; x++) {
if (uniql.indexOf(str.charAt(x)) == -1) {
uniql += str[x];
}
}
return uniql;
}
console.log(unique_char("the fox news newspaper"));
this prints to console: 这打印到控制台:
the foxnwspar
foxnwspar
but my desired output is: 但我想要的输出是:
the fox nws par
狐狸nw par
Any help will be appreciated. 任何帮助将不胜感激。
Thanks 谢谢
Add logical OR
to check if the char is whitespace 添加逻辑
OR
以检查char是否为空格
if (uniql.indexOf(char) == -1 || char == ' ')
See working snippet below: 请参阅下面的工作代码段:
function unique_char(str) { var uniql = ""; for (var x = 0; x < str.length; x++) { var char = str.charAt(x); if (uniql.indexOf(char) == -1 || char == ' ') { uniql += str[x]; } } return uniql; } document.write(unique_char("the fox news newspaper"));
if you're trying to preserve the whitespace, first check to see if it is ' '
, else then check if it's unique. 如果你想保留空格,首先检查它是否是
' '
,否则检查它是否是唯一的。
function unique_char(str1) { var str = str1; var uniql = ""; for (var x = 0; x < str.length; x++) { if (str.charAt(x) === ' '){ uniql += str[x]; } else if (uniql.indexOf(str.charAt(x)) == -1) { uniql += str[x]; } } return uniql; } console.log(unique_char("the fox news newspaper"));
this prints to console: 这打印到控制台:
the fox nws par
狐狸nw par
why not just remove the space? 为什么不只是删除空间?
function unique_char(str1) { var str = str1.replace(/\\s+/g, '');; var uniql = ""; for (var x = 0; x < str.length; x++) { if (uniql.indexOf(str.charAt(x)) == -1) { uniql += str[x]; } } return uniql; } alert(unique_char("the fox news newspaper"));
I would go for using hashsets as their lookup is O(1), instead of N as indexOf()
is. 我会去使用hashsets,因为它们的查找是O(1),而不是像
indexOf()
那样的N. JavaScript has a nice object called Set
. JavaScript有一个很好的对象叫做
Set
。 Alternatively you can use objects as their keys are unique by nature. 或者,您可以使用对象,因为它们的键本质上是唯一的。
For the real question, just skip the char in the string which is a space (equal to ' '
) 对于真正的问题,只需跳过字符串中的字符即空格(等于
' '
)
function unique_char(str1) {
var result = {};
for (var i = 0; i < str1.length; i++) {
if (str1[i] != ' ') result[str1[i]] = 1;
}
return Object.keys(result).join("");
}
console.log(unique_char("the fox news newspaper"));
You can init uniql with a space character and delete it at the end 您可以使用空格字符初始化uniql并在结尾处将其删除
function unique_char(str1) {
var str = str1;
var uniql = " ";
for (var x = 0; x < str.length; x++) {
if (uniql.indexOf(str.charAt(x)) == -1) {
uniql += str[x];
}
}
return uniql[1:];
}
console.log(unique_char("the fox news newspaper"));
if (uniql.indexOf(str.charAt(x)) == -1) {
uniql += (str[x] !== ' ') ? str[x] : '';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.