借助前三个字符在 Javascript 中查找字符串的索引
Find the index of a string in Javascript with help of first three characters
我有很多 tsv 文件,每个文件都有 header 行。现在 header 行中的一列名称是 age
。在少数文件中,列名是 age
而在其他文件中它具有 EOL 字符,例如 \r
\n
.
现在我如何使用 str.indexOf('age')
函数来获取年龄索引,而不管列名年龄是否带有 EOL 字符,例如 \n
、 \r
等。
敌人例如:
tsv file1
:
Name Address Age Ph_Number
file 2
:
Name Address Age/r
file 3
:
Name Address Age\n
我试图在每个文件 header 行中找到 age
列的索引。
但是当我这样做时-
header.indexOf('age')
它只在 file1 的情况下给我结果,因为在其他 2 个文件中我们有 age
作为 age\r
和 age\n
..
我的问题是我应该如何找到 age
的索引,而不考虑 \r
\n
字符以及 header 行中的 age
。
我现在有以下脚本:
var headers = rows[0].split('\t');
if (file.name === 'subjects.tsv'){
for (var i = 0; i < rows.length; i++) {
var ageIdColumn = headers.indexOf("age");
console.log(headers)
正如我在评论中所说,indexOf()
returns 字符串的起始位置。不管它后面是什么:
var csvFile1 = 'column1,column2,column3,age,c1r1';
var csvFile2 = 'column1,column2,column3,age\r,c1r1';
var csvFile3 = 'column1,column2,column3,age\n,c1r1';
console.log(csvFile1.indexOf("age"));
console.log(csvFile2.indexOf("age"));
console.log(csvFile3.indexOf("age"));
如果您特别想查找带有特殊字符的版本,只需明确查找即可:
var csvFile4 = 'column1,age\r,column2,column3,age\n,c1r1';
console.log(csvFile4.indexOf("age\r"));
console.log(csvFile4.indexOf("age\n"));
最后,您可能对 indexOf()
究竟应该做什么感到困惑。它不应该告诉您给定字符串的所有出现位置。它会在第一场比赛结束后停止寻找。要获取所有位置,您需要一个类似于此的循环:
var csvFile5 = 'column1,age\r,column2,age, column3,age\n,c1r1';
var results = []; // Found indexes will be stored here.
var pos = null; // Stores the last index position where "age" was found
while (pos !== -1){
// store the index where "age" is found
// If pos is not null, then we've already found age earlier and we
// need to start looking for the next occurence 3 characters after
// where we found it last time. If pos is null, we haven't found it
// yet and need to start from the beginning.
pos = csvFile5.indexOf("age", pos != null ? pos + 3 : pos );
pos !== -1 ? results.push(pos) : "";
}
// All the positions where "age" was in the string (irrespective of what follows it)
// are recorded in the array:
console.log(results);
我有很多 tsv 文件,每个文件都有 header 行。现在 header 行中的一列名称是 age
。在少数文件中,列名是 age
而在其他文件中它具有 EOL 字符,例如 \r
\n
.
现在我如何使用 str.indexOf('age')
函数来获取年龄索引,而不管列名年龄是否带有 EOL 字符,例如 \n
、 \r
等。
敌人例如:
tsv file1
:
Name Address Age Ph_Number
file 2
:
Name Address Age/r
file 3
:
Name Address Age\n
我试图在每个文件 header 行中找到 age
列的索引。
但是当我这样做时-
header.indexOf('age')
它只在 file1 的情况下给我结果,因为在其他 2 个文件中我们有 age
作为 age\r
和 age\n
..
我的问题是我应该如何找到 age
的索引,而不考虑 \r
\n
字符以及 header 行中的 age
。
我现在有以下脚本:
var headers = rows[0].split('\t');
if (file.name === 'subjects.tsv'){
for (var i = 0; i < rows.length; i++) {
var ageIdColumn = headers.indexOf("age");
console.log(headers)
正如我在评论中所说,indexOf()
returns 字符串的起始位置。不管它后面是什么:
var csvFile1 = 'column1,column2,column3,age,c1r1';
var csvFile2 = 'column1,column2,column3,age\r,c1r1';
var csvFile3 = 'column1,column2,column3,age\n,c1r1';
console.log(csvFile1.indexOf("age"));
console.log(csvFile2.indexOf("age"));
console.log(csvFile3.indexOf("age"));
如果您特别想查找带有特殊字符的版本,只需明确查找即可:
var csvFile4 = 'column1,age\r,column2,column3,age\n,c1r1';
console.log(csvFile4.indexOf("age\r"));
console.log(csvFile4.indexOf("age\n"));
最后,您可能对 indexOf()
究竟应该做什么感到困惑。它不应该告诉您给定字符串的所有出现位置。它会在第一场比赛结束后停止寻找。要获取所有位置,您需要一个类似于此的循环:
var csvFile5 = 'column1,age\r,column2,age, column3,age\n,c1r1';
var results = []; // Found indexes will be stored here.
var pos = null; // Stores the last index position where "age" was found
while (pos !== -1){
// store the index where "age" is found
// If pos is not null, then we've already found age earlier and we
// need to start looking for the next occurence 3 characters after
// where we found it last time. If pos is null, we haven't found it
// yet and need to start from the beginning.
pos = csvFile5.indexOf("age", pos != null ? pos + 3 : pos );
pos !== -1 ? results.push(pos) : "";
}
// All the positions where "age" was in the string (irrespective of what follows it)
// are recorded in the array:
console.log(results);