eclipse 用引号括起来

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/121199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:25:23  来源:igfitidea点击:

Surround with quotation marks

eclipse

提问by Grundlefleck

How is it possible in Eclipse JDT to convert a multiline selection to String. Like the following

如何在 Eclipse JDT 中将多行选择转换为字符串。像下面这样

From:

从:

xxxx
yyyy
zzz

To:

到:

"xxxx " +
"yyyy " +
"zzz"

I tried the following template

我尝试了以下模板

"${line_selection}${cursor}"+

but that way I only get the whole block surrounded not each line separately. How can I achieve a multiline processing like commenting the selected block?

但那样我只能得到整个块,而不是单独地包围每一行。如何实现多行处理,例如注释所选块?

回答by Grundlefleck

Maybe this is not what you mean but...

也许这不是你的意思,但......

If I'm on a line in Eclipse and I enter double quotation marks, then inside that paste a multiline selection (like your xyz example) it will paste out like this:

如果我在 Eclipse 中的一行上并输入双引号,然后在其中粘贴多行选择(如您的 xyz 示例),它将像这样粘贴:

"xxxx\n" +   
"yyyy\n" +  
"zzz"

Then you could just find/replace in a selection for "\n"to "", if you didn't intend the newlines.

然后,如果您不打算换行,您可以在"\n"to的选择中查找/替换""

I think the option to enable this is in Window/Preferences, under Java/Editor/Typing/, check the box next to "Escape text when pasting into a string literal". (Eclipse 3.4 Ganymede)

我认为启用此功能的选项是在Window/PreferencesJava/Editor/Typing/,选中旁边的框"Escape text when pasting into a string literal"。( Eclipse 3.4 Ganymede)

回答by Rafa? Dowgird

Find/Replace with the regex option turned on. Find:

查找/替换打开正则表达式选项。找:

^(.*)$

Replace with:

用。。。来代替:

"" +

Well, the last line will have a surplus +, you have to delete it manually.

好吧,最后一行会有多余的+,你必须手动删除它。

回答by VonC

I would go with a Find/Replace eclipse in regexp mode:

我会在正则表达式模式下使用 Find/Replace eclipse:

  • Find:

    ^((?:\s(?)\S?)((?:\s(?![\r\n])))

  • Replace with

    \1"\2"\3 +

  • 找:

    ^((?:\s(?) \S?)((?:\s(?![\r\n])))

  • 用。。。来代替

    \1"\2"\3 +

Will preserve exactlywhatever space or tabs you have before and after each string, and will surround them with the needed double-quotes. (last '+' needs to be removed)

完全保留每个字符串前后的任何空格或制表符,并用所需的双引号将它们括起来。(最后一个“+”需要去掉)

回答by Diomidis Spinellis

This may not be exactly the answer you're looking for. You can easily achieve what you're asking by using the sed stream editor. This is available on all flavors of Unix, and also on Windows, by downloading a toolkit like cygwin. On the Unix shell command line run the command

这可能不是您正在寻找的确切答案。您可以使用 sed 流编辑器轻松实现您的要求。通过下载像cygwin这样的工具包,这可以在所有版本的 Unix 上使用,也可以在 Windows 上使用。在 Unix shell 命令行上运行命令

sed 's/^/"/;s/$/"+/'

and paste the text you want to convert. On its output you'll obtain the converted text. The argument passed to sed says substitute (s) the beginning of a line (^) with a quote, and substitute (s) the end of each line ($) with a quote and a plus.

并粘贴要转换的文本。在其输出中,您将获得转换后的文本。传递给 sed 的参数表示用引号替换 (s) 行 (^) 的开头,并用引号和加号替换 (s) 每行 ($) 的结尾。

If the text you want to convert is large you may want to redirect sed's input and output through files. In such a case run something like

如果您要转换的文本很大,您可能需要通过文件重定向 sed 的输入和输出。在这种情况下,运行类似

   sed 's/^/"/;s/$/"+/' <inputfile >outputfile

On Windows you can also use the winclip command of the Outwittool suite to directly change what's in the clipboard. Simply run

在 Windows 上,您还可以使用Outwit工具套件的 winclip 命令直接更改剪贴板中的内容。只需运行

winclip -p | sed 's/^/"/;s/$/"+/' | winclip -c

The above command will paste the clipboard's contents into sed and the result back into the clipboard.

上面的命令会将剪贴板的内容粘贴到 sed 中,并将结果粘贴回剪贴板。

Finally, if you're often using this command, it makes sense placing it into a shell script file, so that you can easily run it. You can then even assign an Eclipse keyboard shortcut to it.

最后,如果你经常使用这个命令,把它放在一个 shell 脚本文件中是有意义的,这样你就可以很容易地运行它。然后,您甚至可以为其分配 Eclipse 键盘快捷键。