-2

I have a string like

string variable1="EXAMPLE";

and later somewhere in my code, I use like

 Console.WriteLine(variable1.ToLower());

I may use variable1.ToLower() multiple times. But now I want to store the variablename that is converted to Lower in a separate new variable, that is, I have to extract variable1 from Console.WriteLine(variable1.ToLower()); line and store it in a string variable. Is that possible?

My main goal is that, If my code has variable1.ToLower() in too many places, then I have to run an application, that replaces all variable1.ToLower() to a new string that has the value of variable1.ToLower(). Please Note that using too many variable1.ToLower() in a code is a violation.So I am just creating a new variable to store the value of variable1.ToLower() and use that new variable instead of variable1.ToLower() in every place.

Vedh
  • 93
  • 1
  • 10
  • i don't think `ToLower()` modify your original string, it will create a new instance. try `string lowerCaseString = variable1.ToLower()` – Alex Jan 24 '17 at 07:02
  • I think its better just store the string in a public variable than retrieve it from `Console.WriteLine(variable1.ToLower());` – Hexxed Jan 24 '17 at 07:05
  • 2
    You want to extract the literal text `variable1` from that expression? Sounds like an XY question. What is your end goal here? Why do you need the name of the variable ? – Rob Jan 24 '17 at 07:05
  • @Rob I am using too many variables in my code. And I just want to get the variable that is converted to Lowercase. – Vedh Jan 24 '17 at 07:14
  • So you are looking for a simple find & replace? You can do that with any text editor, even Notepad. – Zohar Peled Jan 24 '17 at 07:31

2 Answers2

1

Assuming I understand the question, why not just do this?

var lower = variable1.ToLower();
Console.WriteLine(lower);
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • There are too many strings in my code and I have to capture only the string that is converted to lower case. Can I extract a string or a phrase that is present before `.ToLower` in a line? – Vedh Jan 24 '17 at 07:11
  • 1
    I guess I don't understand the question after all. What is it that you want to do exactly? what is your goal? – Zohar Peled Jan 24 '17 at 07:19
1

String.ToLower creates a copy of the original string. So the original string is not modified and you can safely use it otherwise.

string variable1 = "EXAMPLE";
string lowerCaseVariable1 = variable1.ToLower();
Console.WriteLine($"Is still the original string: {variable1}");
Console.WriteLine($"Is the lower case copy of the original string: {lowerCaseVariable1}");

EDIT:

If you want to get the name of the string variable instead of the content, you can use nameof (Link).

string variable1 = "EXAMPLE";
string nameOfVariable1 = nameof(variable1);
Console.WriteLine(variable1.ToLower());
gartenriese
  • 4,131
  • 6
  • 36
  • 60
  • Can I extract a string or a phrase that is present before `.ToLower` in a line? Will Regex help? – Vedh Jan 24 '17 at 07:13
  • @Vishwaroopa: You mean something like `nameof(variable1)` ([Link](https://msdn.microsoft.com/en-us/library/dn986596.aspx))? – gartenriese Jan 24 '17 at 07:14