有没有办法插入N次相同的字符 [英] Is there a way to insert N times the same characters

查看:35
本文介绍了有没有办法插入N次相同的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到 vscode 是否有这样的失败.有没有办法构造一个包含 N 个字符的字符串?我自己解释:我需要像这样写一个空字符串:

foobar = "1111111111111111";

有 16 个字符1".有没有办法像在 Vim 中一样构造这样的行:我写了'foobar = "'然后我会发出一个命令,将字符 'i' 重复 16 次.

希望你能明白.

解决方案

这里有一个简单的方法使用

HyperSnips 片段:

snippet `"(.+)\*(\d+)=` "expand";一种``让 origStr = m[1];让 howMany = parseInt(m[2]);让 newStr = origStr.repeat(howMany);rv=`"${newStr}```片尾

此代码位于 .hsnips 文件中,使其仅以该语言运行,或者 all.hsnips 以在所有文件中运行.

我使用这个键让它在 "" 中运行:(.+)\*(\d+)=

= 实际上是触发器 - 它会自动运行 - 您可以将其更改为其他内容.[如果您不关心重复的数字,密钥可能会更短.]

有关设置 HyperSnips(非常简单)的更多信息,请参阅 VSCode 高级自定义代码段

I can't find if vscode has a such failure. Is there a way to construct a string with N characters? I explain myselft: I need to wrote an empty string like this:

foobar = "1111111111111111";

There is 16 times characters '1'. Is there a way, like in Vim, to construct the line like this: i wrote 'foobar = "' then i'd make a command to repeat 16 times the character 'i'.

I hope it's clear for you.

解决方案

Here is an easy way using HyperSnips - a snippet extension that can use javascript to produce the output. First the demo:

The HyperSnips snippet:

snippet `"(.+)\*(\d+)=` "expand" A
``
let origStr = m[1];
let howMany = parseInt(m[2]);
let newStr = origStr.repeat(howMany);
rv=`"${newStr}`
``
endsnippet

This code goes inside a <yourLanguage>.hsnips file to have it run only in that language or all.hsnips to run in all files.

I made it to run inside a "" using this key: (.+)\*(\d+)=

The = is really the trigger - it runs automatically - you could change that to be something else. [The key could be shorter if you didn't care about digits being repeated.]

For more on setting up HyperSnips (which is pretty easy) see VSCode Advanced Custom Snippets

这篇关于有没有办法插入N次相同的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆