在JavaScript中以N的倍数打印字符串的字符

JavaScript string manipulation is a skill that lays down the basis of various of JavaScript DOM string manipulations. In the browser, or on the server, using NodeJS, you might want to work with strings, so it is important that you should know how to work with strings in JavaScript?

JavaScript字符串操作是一项技能,它奠定了各种JavaScript DOM字符串操作的基础。 在使用NodeJS的浏览器或服务器上,您可能想使用字符串,因此了解如何在JavaScript中使用字符串很重要?

In this program, we will look at a function that will return the output string that has n number of skipped characters in the given string. This program tells you about the string manipulation in JavaScript and explores string manipulation basics in JavaScript for you.

在此程序中,我们将看一个函数,该函数将返回给定字符串中具有n个跳过字符的输出字符串。 该程序向您介绍JavaScript中的字符串操作,并为您探索JavaScript中的字符串操作基础知识。

Code JavaScript

代码JavaScript

function stringSkipper(string, skip) {
  var output = "";
  if(string) {
    for(var index=0; index<string.length; index++) {
      if(index % skip === 0) {
        output += stringText[index];
      }
    }
    return output;
  } else {
    return false;
  }
}

var stringText = "This is a random string to check the program";
var skipValue = 3;

var outputString = stringSkipper(stringText, skipValue);
console.log(outputString);

First of all, we defined the function which will perform out string manipulation. In this function, we expect 2 parameters, string and skip. Inside this function, we have an output variable which is set to empty initially.

首先,我们定义了将执行字符串操作的函数。 在此函数中,我们需要2个参数,字符串和跳过。 在此函数内部,我们有一个输出变量,该变量最初设置为空。

Then we check in if the condition that we have a non-empty string parameter, an empty string is a false value. Then we loop through the length of the string and inside that loop, we check if the index is a multiple of skip parameter, by using the modulus operator. If it is true, we simply put this character into the output variable. At the end of the loop, we return the output. In the else part, it will return false, since we get an empty string in the parameter.

然后,我们检查是否有一个非空字符串参数(空字符串)为假值的条件。 然后,我们遍历字符串的长度,并在该循环内,使用模运算符检查索引是否为skip参数的倍数。 如果为真,我们只需将此字符放入输出变量即可。 在循环的最后,我们返回输出。 在else部分中,它将返回false,因为我们在参数中得到了一个空字符串。

Outside of this function, we take two variables, stringText and skipValue for our function parameters. Finally, we call our function and store it a variable since we need the output on the console.

在此函数之外,我们将两个变量stringText和skipValue用作函数参数。 最后,由于需要控制台上的输出,因此我们调用函数并将其存储为变量。

Finally, we console out the outputString and we can see the output. The output, if you notice, is every third character in the string. If you don't need the first character, start the loop from the first index, and that's it, you'll get the expected output.

最后,我们调出outputString,然后可以看到输出。 如果您注意到,输出是字符串中的第三个字符。 如果不需要第一个字符,则从第一个索引开始循环,仅此而已,您将获得预期的输出。

I hope this program gives you a brief idea about string manipulation in Javascript. if you like this article, please share your thoughts in the comments below.

我希望该程序可以为您提供有关Java语言中的字符串操作的简要说明。 如果您喜欢本文,请在下面的评论中分享您的想法。

翻译自: https://www.includehelp.com/code-snippets/print-characters-of-a-string-in-multiples-of-n-in-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值