Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7173739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:53:14+00:00 2026-05-28T15:53:14+00:00

If I have the same string of text typed at numerous locations in my

  • 0

If I have the same string of text typed at numerous locations in my program, is there any way I can extract this to a string variable and, at each location where this was originally typed, have the code instead point to this one variable?

For example, I have the following code:

if(File.Exists("C:\\whatever.txt"))
{
    File.Delete("C:\\whatever.txt");
}

Can I refactor that into this:

string s1 = "C:\\whatever.txt";

if(File.Exists(s1))
{
    File.Delete(s1);
}

I know this is what I should have done initially, but lets say I’m getting the logic of a program sorted first, then I come along after to tidy up my code, are there any shortcuts in Visual Studio to allow me to do this or would I need to do so manually?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:53:16+00:00Added an answer on May 28, 2026 at 3:53 pm

    If you use Resharper, you can just select the first string, press CTRL+R+V to introduce a variable. For identical strings, it will ask you if you want both to be replaced by the variable you introduce.

    CTRL+SHIFT+R is another good keyboard shortcut which also shows you other refactoring options like introduce parameter, field, variable etc.

    If you use Visual Studio you really should have Resharper imo!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario where combobox can have same string values. for exa combo
I have a long string, the format will be same of this string but
I have a string property bound to a textbox. On the same screen there
Let's say I have two same strings inside an ArrayList... is there a way
I have a millions of pairs of string of same length which I want
I have these three lists with the same number of elements List<String>competitoinsIDs = new
I have this string: var someString = 23/03/2012; and want to replace all the
I know there are multiple questions here on SO regarding this same issue already
hi have this code which works perfectly with just value: <script type=text/javascript> $(document).ready(function() {
i have this code, but i can't understand why var_dump($jobs[0][0]) return Some Director ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.