[英]errors with charAt method in javascript
I'm working on a small project with javascript. 我正在使用javascript进行小型项目。 I am using charAt method to get the first character in a line from ace editor.
我正在使用charAt方法从ace编辑器获取一行中的第一个字符。 here's my code:
这是我的代码:
function checkFirstChar (lineNo) {
var words = lines[lineNo].split(" ");
var firstChar = words[0].charAt(0);
if (firstChar == "." || firstChar == "#") {
return 0;
}
else return 1;
}
Where lines is an array or strings, which is extracted from the ace editor. 其中lines是数组或字符串,从ace编辑器中提取。 Problem is, this works fine when the first character is '#'.
问题是,当第一个字符为“#”时,这可以正常工作。 But when the first character is '.', it doesn't take it as a dot '.'
但是,当第一个字符为“。”时,不会将其当作点“。”。 Instead, it takes it as a "" (empty string).
而是将其作为“”(空字符串)。 Can anyone help me out here ?
有人可以帮我从这里出去吗 ? Whats wrong with it ?
它出什么问题了 ?
Edit: This is how I took the array, lines. 编辑:这就是我采取的数组,行。
for(var i = 0, lines = new Array(numOfLines); i<numOfLines; i++ ){
lines[i] = editor.session.getLine(i);
}
Most confusing part is, when I make an alert from words[0], it shows the word with the dot "." 最令人困惑的是,当我从单词[0]发出警报时,它显示带有点“”的单词。
I tried the code. 我尝试了代码。 It works perfectly fine for me.
对我来说,它工作得很好。 Please check the values that are being inserted in your array.
请检查插入数组中的值。 I suppose it should work fine.
我想它应该可以正常工作。 I implemented the following code:
我实现了以下代码:
function myFunc() {
var words = ".sdf dfsf #sdsd df";
var firstChar = words[0].charAt(0);
document.getElementById("demo").innerHTML = "";
if (firstChar == "." || firstChar == "#")
document.getElementById("demo").innerHTML = "bingo";
}
Instead of using charAt(), you should use your string as a table of chars. 而不是使用charAt(),您应该将字符串用作char表。
var firstChar = words[0].charAt(0);
can be replaced by 可以替换为
var firstChar = words[0][0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.