首页 > 技术文章 > IntelliJ IDEA使用技巧,复制字符串连接文本到剪切板

gaohanghang 2020-10-30 00:50 原文

比如有以下代码,字符串中是一个查询男生、女生人数的 sql

public class Test {

    public static void main(String[] args) {
        // 查询男生、女生人数
        String sql = "select sex,count(*)" +
                "from student" +
                "group by sex;";
    }

}

image.png

如果我想复制出 sql,一般的做法是先复制出以下内容:

"select sex,count(*)" +
                "from student" +
                "group by sex;"

然后再删除双引号和加号,得到需要的内容

select sex,count(*) from student group by sex;

这样做比较麻烦,使用 idea 有更简便的方法

技巧

将光标置于字符串的值处,然后按Alt + Enter(Mac 是 option + return) ,选择 Copy String Concatenation Text to the Clipboard

image.png

复制结果如下,它会删除所有的 "+ ,注意它不会将其放在1行上,还是会有3行,但已经很方便了。

select sex,count(*)
from student
group by sex;

参考

Copying a concatenated constant to the clipboard

串接(Concatenation)- wikipedia

推荐阅读