为了好玩,我写了一个快速脚本来接受带逗号分隔字符的字符串,我使用逗号来表示不同的单词,然后输出一个带有单词作为元素的数组。我的问题是我的逻辑 - 这只适用于原始字符串中的逗号。
发生的事情是该算法只查看我在调用函数时提供的分隔符数组中的第一个元素(逗号)。我正在考虑在某处使用布尔值作为标志,但不确定这是否真的是最好的方法。有什么建议吗?
var stringToArray = function(delimiterArray, originalString) {
var arrayOutput = []
var tempWord = ""
for (var i = 0; i < originalString.length; i++) {
for (var j = 0; j < delimiterArray.length; j++) {
var currentCharacter = originalString.charAt(i)
var currentDelimiter = delimiterArray[j]
while (currentCharacter != currentDelimiter) {
tempWord += currentCharacter
//once we hit a delimiter, break so that we can move onto the next conditional statement.
break
}
if (currentCharacter === currentDelimiter) {
//push word onto the array to hold each string, and then break out so we can go back to the iteration of the nested loops
arrayOutput.push(tempWord)
tempWord = ""
break
}
//we break out of the second for loop ->
break
}
}
return arrayOutput
}
delims = [',', '.', ';', ' ']
originalString = "USA,Canada,Mexico,Bermuda,Grenada,Belize"
finalOutput = stringToArray(delims, originalString)
console.log(finalOutput)
&#13;
如果我使用&#39;。&#39;作为我用作参数的字符串中的分隔符,我的算法失败了。我使用布尔标志(很久以前)在C中实现了这一点,我必须编写更多代码,因为我没有使用任何内置函数(必须找到所有内容的长度,所以我可以为数据提供足够的内存存储东西的结构)。但是,不想重新访问旧代码。
答案 0 :(得分:0)
问题是,如果当前字符与当前分隔符不匹配,则将当前字符添加到tempWord
。因此,如果当前字符与第一个分隔符不匹配,则会将其添加到tempWord,即使它可能与其他分隔符匹配。在确定字符是否是分隔符之前,您需要遍历整个分隔符数组。
var stringToArray = function(delimiterArray, originalString) {
var arrayOutput = [];
var tempWord = "";
for (var i = 0; i < originalString.length; i++) {
var currentCharacter = originalString.charAt(i)
var isDelimiter = false;
for (var j = 0; j < delimiterArray.length; j++) {
var currentDelimiter = delimiterArray[j]
if (currentCharacter == currentDelimiter) {
isDelimiter = true;
//once we hit a delimiter, break so that we can move onto the next conditional statement.
break;
}
}
if (isDelimiter) {
arrayOutput.push(tempWord);
tempWord = "";
} else {
tempWord += currentCharacter;
}
}
return arrayOutput;
}
delims = [',', '.', ';', ' ']
originalString = "USA,Canada.Mexico,Bermuda,Grenada,Belize"
finalOutput = stringToArray(delims, originalString)
console.log(finalOutput)
出于某种原因,人们编写自己的数组搜索代码似乎是一个非常常见的错误。
答案 1 :(得分:0)
你有几个问题:
isDeliminator()
函数。这使得代码更清晰,更模块化(您可以再次使用它)并且通常可以减少错误。以下是我的代码。
function stringToArray(delimiterArray, originalString) {
var arrayOutput = [];
var tempWord = '';
for (var i = 0; i < originalString.length; i++) {
var currentCharacter = originalString.charAt(i);
if (isDeliminator(currentCharacter)) {
//push word onto the array to hold each string, and then break out so we can go back to the iteration of the nested loops
arrayOutput.push(tempWord);
tempWord = '';
} else {
tempWord += currentCharacter;
}
}
// Push the last work onto the array
arrayOutput.push(tempWord);
return arrayOutput;
}
function isDeliminator(char) {
for (var i = 0; i < delims.length; i += 1) {
if (char === delims[i]) {
return true;
}
}
return false;
}
var delims = [',', '.', ';', ' '];
var inputString = 'USA.Canada,Mexico;Bermuda,Grenada,Belize';
var finalOutput = stringToArray(delims, inputString);
console.log(finalOutput);
答案 2 :(得分:0)
这是一个更正过的脚本,其中包含更改:
var stringToArray = function(delimiterArray, originalString) {
var arrayOutput = []
var tempWord = ""
for (var i = 0; i < originalString.length; i++) {
// Set currentCharacter here, as it does not depend on j:
var currentCharacter = originalString.charAt(i)
for (var j = 0; j < delimiterArray.length; j++) {
var currentDelimiter = delimiterArray[j]
if (currentCharacter === currentDelimiter) {
//push word onto the array to hold each string, and then break out so we can go back to the iteration of the nested loops
arrayOutput.push(tempWord)
tempWord = ""
break
}
//No, we don't break out of the second for loop ->
}
// Moved out of the j-loop, and turned into an IF without BREAK:
if (currentCharacter != currentDelimiter) {
tempWord += currentCharacter
}
}
return arrayOutput
}
var delims = [',', '.', ';', ' ']
var originalString = "USA,Canada,Mexico,Bermuda,Grenada,Belize"
var finalOutput = stringToArray(delims, originalString)
document.write(JSON.stringify(finalOutput))