# 查找并替换代码重复项

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com.cn/help/idea/llms.txt).

查找并替换代码重复项   操作允许您查找与所选方法或常量字段相似的代码重复，并将其替换为对原始方法或常量的调用。 它还允许您为搜索指定范围。

Procedure:

1. 将文本光标放在您想要搜索重复项的方法或常量字段中。

2. 从主菜单或右键菜单中选择  `重构 | 查找并替换代码重复` 。

3. 在打开的对话框中，选择 IntelliJ IDEA 应该查找代码重复项的范围。

4. 对于每个发现的代码重复项，IntelliJ IDEA 将提示您确认替换。

## 示例

|  之前  |  之后  |
| --- | --- |
|     ```JAVA public void method() {     int a = 1;     int b = 2;     int c = a+b;     int d = b+c; } ... private int add(int a, int b) {     return a+b; }  ```    |     ```JAVA public void method() {     int a = 1;     int b = 2;     int c = add(a,b);     int d = add(b,c); } ... private int add(int a, int b) {     return a+b; }  ```    |

