javascript 通过char javascript遍历字符串char
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25490030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
traverse a string char by char javascript
提问by Michael Sacks
function SimpleSymbols(str) {
var letter =['a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var newstr = "";
for (var i = 0; i<str.length; i++){
if (str.charAt(i).toLowerCase() in letter){
newstr += "M";
}
else{
newstr += "X";
}
}
return newstr;
}
If str is "Argument goes here" it returns XXXXXXXXX. WHy doesn't it return MMMMMMMMMM?
如果 str 是“参数在此处”,则返回 XXXXXXXXX。为什么它不返回 MMMMMMMMMM?
回答by dreamlab
you do not look up an entry in an array with in
. use indexOf()
to find the position of an array entry. indexOf()
will return the position or -1
if no entry is found.
您不会使用in
. 用于indexOf()
查找数组条目的位置。indexOf()
将返回位置或者-1
如果没有找到条目。
for (var i = 0; i<str.length; i++){
var strChar = str.charAt(i).toLowerCase();
if ( letter.indexOf(strChar) >= 0 ) {
newstr += "M";
}
…
回答by Felix Kling
The in
operatorreturns true
if the object has a property with that name, not with that value.
该in
运营商将返回true
如果对象有一个属性的名称,而不是与价值。
An array is basically an object with numeric properties. I.e. the indexesare the property names of the object. It basically looks like this:
数组基本上是一个具有数字属性的对象。即索引是对象的属性名称。它基本上是这样的:
var letters = {
0: 'a',
1: 'b',
...
length: ...
};
So in your case the condition will only be true
if str.charAt(i).toLowerCase()
returns a number between 0
and letter.length
(and since charAt
only returns one character, it can only be 0-9
).
所以你的情况的情况只会是true
,如果str.charAt(i).toLowerCase()
回报率之间的数字0
和letter.length
(和因为charAt
只有回到一个字符,它只能是0-9
)。
Example:
例子:
> var letters = ['a', 'b', 'c'];
> 'a' in letters // array doesn't have a property 'a'
false
> 0 in letters // array has a property 0 (it's the first element)
true
So since, "Argument goes here"
doesn't contain any digits, the in
condition will always be false
and that's why you get XXXXXX...
as result.
因此,由于"Argument goes here"
不包含任何数字,in
条件将始终是false
,这就是您得到XXXXXX...
结果的原因。
See the question "How do I check if an array includes an object in JavaScript?" for testing the existence of an element in an array.
请参阅问题“如何检查数组是否包含 JavaScript 中的对象?”以测试数组中元素的存在。
FWIW, to make the in
operator work, you would have to create an object of the form:
FWIW,要使in
操作员工作,您必须创建以下形式的对象:
var letters = {
'a': true,
'b': true,
// ...
};
but that's a bit cumbersome to write.
但是写起来有点麻烦。
回答by Dalorzo
Allow me to offer a side view, another way handle what I think you intent to do by using Regular Expressionswith something like:
请允许我提供一个侧视图,另一种方法可以通过使用正则表达式来处理我认为您打算做的事情,例如:
"test2".replace(/[a-z]/gi,"M").replace(/[^M]/g,"X") //Outputs "MMMMX"
"test2".replace(/[a-z]/gi,"M").replace(/[^M]/g,"X") //Outputs "MMMMX"
String.replace
will replace an string that contains letters from [a-z]
the i
at the end of the expression means case insensitive. g means will search for all possible matches and not just the first match. In the second expression [^M]
this ^
means negation so anything that is not an M
will be replaced with X
.
String.replace
将取代包含从信件串[a-z]
的i
在表达手段不区分大小写的端部。g 表示将搜索所有可能的匹配项,而不仅仅是第一个匹配项。在第二个表达式中,[^M]
这^
意味着否定,因此任何不是 an 的M
都将替换为X
。
There is another way in which we implement a custom function within the String.replace
using Regular Expressions and it can be implemented like this:
还有另一种方法可以在String.replace
using 正则表达式中实现自定义函数,它可以像这样实现:
"test2".replace(/([a-z])|([^a-z])/gi,
function(m,g1, g2){
return g1 ? "M" : "X";
});
In regular expression parenthesis creates groups and | means or in this expression ([a-z])|([^a-z])
there 2 groups one with letters from a-z and the other which means everything that is not a-z
with the replace function we asked only for group g1
if it is group 1 is M
otherwise is an X
.
在正则表达式括号中创建组和 | 表示或在此表达式中([a-z])|([^a-z])
有 2 个组,一个是来自 az 的字母,另一个是指所有不a-z
具有替换功能的内容,g1
如果它是组 1,则我们只要求group 1M
否则是 an X
。
Another cool thing you could do is add this function to all your string by prototyping it like:
你可以做的另一件很酷的事情是通过像这样的原型设计把这个函数添加到你的所有字符串中:
String.prototype.traverse = function(){ return this.replace(/([a-z])|([^a-z])/gi,function(m,g1){ return g1 ? "M" : "X" });}
Then it can be used as simple as: "test1".traverse();
然后它可以像这样简单地使用: "test1".traverse();