J4102
J4102

Reputation: 473

How to refactor only certain area in Android Studio?

How would I refactor only certain areas of code in Android Studios that are commonly called? For instance:

Dog dog = new Dog();
dog.bark()

Dog dog = new Dog();
dog.bark()

Dog dog = new Dog();
dog.bark()

Dog dog = new Dog();
dog.bark()

I want to be able to change the 2nd copy of the dog object into dog1 and use dog1.bark instead of dog.bark(). I also want to rename the 3rd dog object to dog3 and call dog3.bark instead. Is there any shortcut that allows me to rename only certain portions of code in android studios?

Upvotes: 0

Views: 309

Answers (2)

Pratik Butani
Pratik Butani

Reputation: 62401

The easiest way is:

Dog dog = new Dog();
dog.bark();

Dog dog1 = new Dog();
dog1.bark();

Dog dog2 = new Dog();
dog2.bark();

Dog dog3 = new Dog();
dog3.bark();

Manually as I have done, you can also do same, because there is no way to create variables as you want in sequence.

Right now you can copy/paste my code but if you want to do for more objects you can do manually because it will be better.

Upvotes: 0

Masoud Mokhtari
Masoud Mokhtari

Reputation: 2474

Just select area that you want and then with command ctrl+R open window replace and checked In Selection then type what you want to replace and enjoy from that.

Like this image

Upvotes: 1

Related Questions