从字符串中删除“,”(和 return RGB\n 字符串)
Delete "," from Strig (and return RGB\n String)
所以我在这个问题上卡了几天:
我的输入是这样的:
"255,255,255,10,251,91,31,4,220,220,220,1"
它是一个具有 3 个不同 RGB 值的字符串,它还带有一个数字,表示它们的数量从高到低。
您可以将上面的字符串翻译成:
"RED1, GREEN1, BLUE1, QUANTITY1, RED2, GREEN2 ... "
虽然 1 代表第一种颜色,2 代表第二种颜色,依此类推。
我需要return的是没有数量的第一种颜色。
所以我的输出应该是这样的:
"255,255,255,251,91,31,220,220,220"
我试过各种各样的东西,其中一个是这个功能:
var firstC, secondC, thirdC;
var pointer = "firstC";
function getEachColorValue(color) {
var startIndex = 0;
var endIndex = 0;
for (var i = 0; i < color.length; i++) {
if (color.charAt(i) == ",") {
endIndex = i;
if (pointer == "firstC") {
firstC = color.substring(startIndex, endIndex);
startIndex = endIndex + 1;
pointer = "secondC";
} else if (pointer == "secondC") {
secondC = color.substring(startIndex, endIndex);
startIndex = endIndex + 1;
pointer = "thirdC";
} else if (pointer == "thirdC") {
thirdC = color.substring(startIndex, endIndex);
startIndex = endIndex;
pointer = "firstC";
}
}
}
}
这会在 firstC
中推送 RED1
,在 secondC
中推送 GREEN1
,在 thirdC
中推送 BLUE1
有一次想过,用一个函数把firstC, secondC, thirdC写成一个数组,重新设置。然后把col3
剪成没有第一个色盘的子串,然后重复
// Global variables
var stringHolder;
var newString;
var counter = 0;
var colorSetHT = new Array;
// main
createSubstring(col3);
function createSubstring(color) {
var startIndex = 0;
var endIndex = 0;
for (var i = 0; i < color.length; i++) {
if (color.charAt(i) == ",") {
counter++;
endIndex = i;
}
if (counter == 4) {
stringHolder = color.substring(startIndex, endIndex);
alert(stringHolder);
newString = color.substring(endIndex+1, color.length);
getEachColorValue(stringHolder);
colorSetHT.push(firstC, secondC, thirdC)
colorReset();
counter = 0;
stringHolder = "";
// createSubstring(newString); // ?
}
}
}
我试过这个,但到目前为止运气不好。我什至试图递归地做。
我对 Javascript 有点陌生(实际上是为 Extendscript 做的),我认为有一种更简单的方法,使用 split/slice
但我还没有找到。我努力让它尽可能简单快速地阅读,如果我能提供任何进一步的信息,请告诉我,在此先感谢!
这里是 split
的操作方法。
var input = "255,255,255,10,251,91,31,4,220,220,220,1";
var inputArray = input.split(",");
var outputArray = [];
for(let i = 0;i<inputArray.length;i++)
{
if(i%4 != 3)
{
outputArray.push(inputArray[i]);
}
}
var output = outputArray.join(",");
console.log(output);
尝试
let output = input.split(',').filter((x,i)=> i%4-3).join();
let input="255,255,255,10,251,91,31,4,220,220,220,1"
let output = input.split(',').filter((x,i)=> i%4-3).join();
console.log(output);
像这样使用简单的 for
循环:
const str = "255,255,255,10,251,91,31,4,220,220,220,1";
var colors = str.split(",");
var x = Math.floor(colours.length / 4);
while (x--) {
colors.splice((x + 1) * 4 - 1, 1);
}
colors = colors.join(",");
console.log(colors);
所以我在这个问题上卡了几天: 我的输入是这样的:
"255,255,255,10,251,91,31,4,220,220,220,1"
它是一个具有 3 个不同 RGB 值的字符串,它还带有一个数字,表示它们的数量从高到低。 您可以将上面的字符串翻译成:
"RED1, GREEN1, BLUE1, QUANTITY1, RED2, GREEN2 ... "
虽然 1 代表第一种颜色,2 代表第二种颜色,依此类推。
我需要return的是没有数量的第一种颜色。 所以我的输出应该是这样的:
"255,255,255,251,91,31,220,220,220"
我试过各种各样的东西,其中一个是这个功能:
var firstC, secondC, thirdC;
var pointer = "firstC";
function getEachColorValue(color) {
var startIndex = 0;
var endIndex = 0;
for (var i = 0; i < color.length; i++) {
if (color.charAt(i) == ",") {
endIndex = i;
if (pointer == "firstC") {
firstC = color.substring(startIndex, endIndex);
startIndex = endIndex + 1;
pointer = "secondC";
} else if (pointer == "secondC") {
secondC = color.substring(startIndex, endIndex);
startIndex = endIndex + 1;
pointer = "thirdC";
} else if (pointer == "thirdC") {
thirdC = color.substring(startIndex, endIndex);
startIndex = endIndex;
pointer = "firstC";
}
}
}
}
这会在 firstC
中推送 RED1
,在 secondC
中推送 GREEN1
,在 thirdC
BLUE1
有一次想过,用一个函数把firstC, secondC, thirdC写成一个数组,重新设置。然后把col3
剪成没有第一个色盘的子串,然后重复
// Global variables
var stringHolder;
var newString;
var counter = 0;
var colorSetHT = new Array;
// main
createSubstring(col3);
function createSubstring(color) {
var startIndex = 0;
var endIndex = 0;
for (var i = 0; i < color.length; i++) {
if (color.charAt(i) == ",") {
counter++;
endIndex = i;
}
if (counter == 4) {
stringHolder = color.substring(startIndex, endIndex);
alert(stringHolder);
newString = color.substring(endIndex+1, color.length);
getEachColorValue(stringHolder);
colorSetHT.push(firstC, secondC, thirdC)
colorReset();
counter = 0;
stringHolder = "";
// createSubstring(newString); // ?
}
}
}
我试过这个,但到目前为止运气不好。我什至试图递归地做。
我对 Javascript 有点陌生(实际上是为 Extendscript 做的),我认为有一种更简单的方法,使用 split/slice
但我还没有找到。我努力让它尽可能简单快速地阅读,如果我能提供任何进一步的信息,请告诉我,在此先感谢!
这里是 split
的操作方法。
var input = "255,255,255,10,251,91,31,4,220,220,220,1";
var inputArray = input.split(",");
var outputArray = [];
for(let i = 0;i<inputArray.length;i++)
{
if(i%4 != 3)
{
outputArray.push(inputArray[i]);
}
}
var output = outputArray.join(",");
console.log(output);
尝试
let output = input.split(',').filter((x,i)=> i%4-3).join();
let input="255,255,255,10,251,91,31,4,220,220,220,1"
let output = input.split(',').filter((x,i)=> i%4-3).join();
console.log(output);
像这样使用简单的 for
循环:
const str = "255,255,255,10,251,91,31,4,220,220,220,1";
var colors = str.split(",");
var x = Math.floor(colours.length / 4);
while (x--) {
colors.splice((x + 1) * 4 - 1, 1);
}
colors = colors.join(",");
console.log(colors);