Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 跳过x个字符后,如何打印特定字符串?_Javascript_Function - Fatal编程技术网

Javascript 跳过x个字符后,如何打印特定字符串?

Javascript 跳过x个字符后,如何打印特定字符串?,javascript,function,Javascript,Function,我想打印下面文本中的嘿。我需要创建一个函数,从文本中跳过一定数量的字符来打印hey中的字母。因此,在这种情况下,它将跳过0个字符来打印h,然后跳过2个字符(a和b)来打印e,并跳过1个字符(z)来打印y var text = ["0h2abe1zy"] var decode = function () { for(var i=0; i < text.length; i++) { if (text[i]!==isNaN){ console.log( ) };

我想打印下面文本中的
。我需要创建一个函数,从文本中跳过一定数量的字符来打印
hey
中的字母。因此,在这种情况下,它将跳过0个字符来打印
h
,然后跳过2个字符(
a
b
)来打印
e
,并跳过1个字符(
z
)来打印
y

var text = ["0h2abe1zy"]
var decode = function () {
  for(var i=0; i < text.length; i++) {
    if (text[i]!==isNaN){
      console.log( )
    };
  };
};
var text=[“0h2abe1zy”]
var decode=函数(){
对于(变量i=0;i

这是到目前为止我的代码,我用if语句找到了数字,但我不知道如何告诉它跳过那么多字符并打印以下字符。

尝试以下函数。函数注释中的解释

var decode = function (text) {
  var output = "";
  var i = 0;
  while (i < text.length) {
    if (/\D/.test(text[i])){  // if character at current index is not a number
      output += text[i];      // add it to output
    } else {                  // otherwise 
      i += +text[i];          // add that number to current index in order
    }                         // to skip that many characters
    i++;
  };
  return output;
};

decode("0h2abe1zy");     // "hey"
decode("3jyhf0i2ikn0d"); // "find"
decode("0He3abcl14lo2gh 3zxyw1ior5abcdeld"); // "Hello world"
顺便说一下,我假设您的输出中不需要任何数字。如果您确实希望能够输出数字,您可以通过假设跳过的字符后面的任何字符都将始终输出,即使它本身是数字(因此
解码(“010203”)
将输出
“123”
)。对
else
案例的一个小更改将处理以下情况:

} else {                  // otherwise
  i += +text[i] + 1;      // add that number to current index and
  output += text[i];      // immediately output the next character
}
这将使:

decode("1aW0e3abc'0r1ae2aa 0n1uum4zaefb0e1sr1i 2is10!") // "We're number 1!"

或者将其与s结合使用,允许一次跳过超过9个字符。

尝试以下功能。函数注释中的解释

var decode = function (text) {
  var output = "";
  var i = 0;
  while (i < text.length) {
    if (/\D/.test(text[i])){  // if character at current index is not a number
      output += text[i];      // add it to output
    } else {                  // otherwise 
      i += +text[i];          // add that number to current index in order
    }                         // to skip that many characters
    i++;
  };
  return output;
};

decode("0h2abe1zy");     // "hey"
decode("3jyhf0i2ikn0d"); // "find"
decode("0He3abcl14lo2gh 3zxyw1ior5abcdeld"); // "Hello world"
顺便说一下,我假设您的输出中不需要任何数字。如果您确实希望能够输出数字,您可以通过假设跳过的字符后面的任何字符都将始终输出,即使它本身是数字(因此
解码(“010203”)
将输出
“123”
)。对
else
案例的一个小更改将处理以下情况:

} else {                  // otherwise
  i += +text[i] + 1;      // add that number to current index and
  output += text[i];      // immediately output the next character
}
这将使:

decode("1aW0e3abc'0r1ae2aa 0n1uum4zaefb0e1sr1i 2is10!") // "We're number 1!"
或者将其与s结合使用,允许一次跳过超过9个字符。

如果与的答案不同,您希望将相邻数字作为一个整数进行分析,则可以使用

function decode(string) {
    var filtered = [],
        skip = 0;
    for(var index = 0; index < string.length; index++) {
        var character = string[index];
        if(character >= '0' && character <= '9') // It's a digit
            skip = skip * 10 + (+character);
        else if (skip) index += skip-1, skip = 0;
        else filtered.push(character);
    }
    return filtered.join("");
}
如果您想将相邻的数字作为一个整数进行解析,则可以使用

function decode(string) {
    var filtered = [],
        skip = 0;
    for(var index = 0; index < string.length; index++) {
        var character = string[index];
        if(character >= '0' && character <= '9') // It's a digit
            skip = skip * 10 + (+character);
        else if (skip) index += skip-1, skip = 0;
        else filtered.push(character);
    }
    return filtered.join("");
}
这很有效

    function decode(text) {
        var result = '';
        var i = 0, n = text.length;
        while (i < n) {
            if (Number(text[i]) == text[i]) {
                i += parseInt(text[i]) + 1;
            }
            else
                result += text[i++];
        }
        return result;
    }
var result = decode("0h2abe1zy");
函数解码(文本){
var结果=“”;
变量i=0,n=text.length;
而(i
如果您有一个数组
[“0h2abe1zy”]
,则应用于每个成员。
这是你想要的吗?

这很有效

    function decode(text) {
        var result = '';
        var i = 0, n = text.length;
        while (i < n) {
            if (Number(text[i]) == text[i]) {
                i += parseInt(text[i]) + 1;
            }
            else
                result += text[i++];
        }
        return result;
    }
var result = decode("0h2abe1zy");
函数解码(文本){
var结果=“”;
变量i=0,n=text.length;
而(i
如果您有一个数组
[“0h2abe1zy”]
,则应用于每个成员。

这就是你想要的吗?

Hmm,如果你总是需要打印hey word,那么为什么你需要解析源字符串呢?
text
应该是一个数组,其中包含一个字符串元素,还是应该是一个普通字符串?(您的循环似乎将其视为一个普通字符串,或一个由一个字母字符串组成的数组。)我想解码此文本:var text=[“0h2abe1zy”]我需要解码的是一个字符串“0h2abe1zy”,您是否有h,e,yHmm的条件,为什么需要解析源字符串,如果您需要始终只打印hey word?
text
应该是一个数组,其中包含一个字符串元素,还是应该只是一个普通字符串?(您的循环似乎将其视为一个普通字符串,或一个由一个字母字符串组成的数组。)我想解码此文本:var text=[“0h2abe1zy”]我需要解码的是一个字符串“0h2abe1zy”。您是否有h,e,y
isNaN()的条件。它只接受一个数字作为参数。它用于测试值是否真的是
NaN
。协作会产生意想不到的行为。@4castle-你想的不是。我在这里使用的将首先强制其参数为numeric,然后测试它是否为NaN-是的,它有它的问题,但是对于这个
decode()
函数来说,它工作得很好。(正如你所看到的,如果你真的运行我的代码:它可以工作。)当然,但是
isNaN(“”
是false,因此出乎意料。(一个空字符串被强制为0,这是一个数字)@4castle-True。但我想我假设输入不包含空格。我已经更新了我的答案,改为使用正则表达式测试case@AleksanderAzizi-请停止编辑我的答案。你重新表述我的解释的方式过于强调正则表达式——这只是一个小细节,我不想强调它。而且,
newstring()
几乎从来都不是一个好计划。
isNaN()
不会做你认为它会做的事。它只接受一个数字作为参数。它用于测试值是否真的是
NaN
。协作会产生意想不到的行为。@4castle-你想的不是。我在这里使用的将首先强制其参数为numeric,然后测试它是否为NaN-是的,它有它的问题,但是对于这个
decode()
函数来说,它工作得很好。(正如你所看到的,如果你真的运行我的代码:它可以工作。)当然,但是
isNaN(“”
是false,因此出乎意料。(一个空字符串被强制为0,这是一个数字)@4castle-True。但我想我假设输入不包含空格。我已经更新了我的答案,改为使用正则表达式测试case@AleksanderAzizi-请停止编辑我的答案。你重新表述我的解释的方式过于强调正则表达式——这只是一个小细节,我不想强调它。而且
newstring()
几乎从来都不是一个好计划