How to use variable inside interpolated string? [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • String Interpolation With Variable Content in C#

    3 answers




I'm trying to use a variable inside of an interpolated string, but not having much luck. How would I do this?



var name = "mike";
var desc = "hello world {name}";
var t = $"{ desc }";
Console.WriteLine(t); // PRINTS: hello world {name}


This is what I am trying to achieve:



Console.WriteLine(t); // PRINTS: hello world mike


Is this possible?



For example, suppose I have a method:



public string FormatString(string s) {
var Now = DateTime.Now;
return $s;
}


Usage:



Console.WriteLine(FormatString("The time is {Now}"));









share|improve this question















marked as duplicate by mjwills, Nkosi c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 2:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • is there no way of storing a variable (desc), and somehow have it interpreted based on contextual ambient variables?
    – pixelbits
    Nov 22 at 2:05










  • @mjwlls, yup that's a duplicate - that's exactly what i'm trying to do
    – pixelbits
    Nov 22 at 2:17










  • Since its not available within the C# language, does anyone know if there is a library? I know its been recommended to use CSharpScript.EvaluateAsync in the other answer, but I need to be able to pass in complex objects in the ambient context, not just string variables.
    – pixelbits
    Nov 22 at 2:34












  • Actually, never mind. CSharpScript.EvaluateAsync works for me, it supports complex global context parameters.
    – pixelbits
    Nov 22 at 2:43















up vote
0
down vote

favorite













This question already has an answer here:




  • String Interpolation With Variable Content in C#

    3 answers




I'm trying to use a variable inside of an interpolated string, but not having much luck. How would I do this?



var name = "mike";
var desc = "hello world {name}";
var t = $"{ desc }";
Console.WriteLine(t); // PRINTS: hello world {name}


This is what I am trying to achieve:



Console.WriteLine(t); // PRINTS: hello world mike


Is this possible?



For example, suppose I have a method:



public string FormatString(string s) {
var Now = DateTime.Now;
return $s;
}


Usage:



Console.WriteLine(FormatString("The time is {Now}"));









share|improve this question















marked as duplicate by mjwills, Nkosi c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 2:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • is there no way of storing a variable (desc), and somehow have it interpreted based on contextual ambient variables?
    – pixelbits
    Nov 22 at 2:05










  • @mjwlls, yup that's a duplicate - that's exactly what i'm trying to do
    – pixelbits
    Nov 22 at 2:17










  • Since its not available within the C# language, does anyone know if there is a library? I know its been recommended to use CSharpScript.EvaluateAsync in the other answer, but I need to be able to pass in complex objects in the ambient context, not just string variables.
    – pixelbits
    Nov 22 at 2:34












  • Actually, never mind. CSharpScript.EvaluateAsync works for me, it supports complex global context parameters.
    – pixelbits
    Nov 22 at 2:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • String Interpolation With Variable Content in C#

    3 answers




I'm trying to use a variable inside of an interpolated string, but not having much luck. How would I do this?



var name = "mike";
var desc = "hello world {name}";
var t = $"{ desc }";
Console.WriteLine(t); // PRINTS: hello world {name}


This is what I am trying to achieve:



Console.WriteLine(t); // PRINTS: hello world mike


Is this possible?



For example, suppose I have a method:



public string FormatString(string s) {
var Now = DateTime.Now;
return $s;
}


Usage:



Console.WriteLine(FormatString("The time is {Now}"));









share|improve this question
















This question already has an answer here:




  • String Interpolation With Variable Content in C#

    3 answers




I'm trying to use a variable inside of an interpolated string, but not having much luck. How would I do this?



var name = "mike";
var desc = "hello world {name}";
var t = $"{ desc }";
Console.WriteLine(t); // PRINTS: hello world {name}


This is what I am trying to achieve:



Console.WriteLine(t); // PRINTS: hello world mike


Is this possible?



For example, suppose I have a method:



public string FormatString(string s) {
var Now = DateTime.Now;
return $s;
}


Usage:



Console.WriteLine(FormatString("The time is {Now}"));




This question already has an answer here:




  • String Interpolation With Variable Content in C#

    3 answers








c# .net-core roslyn






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 2:51









John

10.7k31735




10.7k31735










asked Nov 22 at 2:03









pixelbits

39.6k1065101




39.6k1065101




marked as duplicate by mjwills, Nkosi c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 2:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by mjwills, Nkosi c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 2:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • is there no way of storing a variable (desc), and somehow have it interpreted based on contextual ambient variables?
    – pixelbits
    Nov 22 at 2:05










  • @mjwlls, yup that's a duplicate - that's exactly what i'm trying to do
    – pixelbits
    Nov 22 at 2:17










  • Since its not available within the C# language, does anyone know if there is a library? I know its been recommended to use CSharpScript.EvaluateAsync in the other answer, but I need to be able to pass in complex objects in the ambient context, not just string variables.
    – pixelbits
    Nov 22 at 2:34












  • Actually, never mind. CSharpScript.EvaluateAsync works for me, it supports complex global context parameters.
    – pixelbits
    Nov 22 at 2:43


















  • is there no way of storing a variable (desc), and somehow have it interpreted based on contextual ambient variables?
    – pixelbits
    Nov 22 at 2:05










  • @mjwlls, yup that's a duplicate - that's exactly what i'm trying to do
    – pixelbits
    Nov 22 at 2:17










  • Since its not available within the C# language, does anyone know if there is a library? I know its been recommended to use CSharpScript.EvaluateAsync in the other answer, but I need to be able to pass in complex objects in the ambient context, not just string variables.
    – pixelbits
    Nov 22 at 2:34












  • Actually, never mind. CSharpScript.EvaluateAsync works for me, it supports complex global context parameters.
    – pixelbits
    Nov 22 at 2:43
















is there no way of storing a variable (desc), and somehow have it interpreted based on contextual ambient variables?
– pixelbits
Nov 22 at 2:05




is there no way of storing a variable (desc), and somehow have it interpreted based on contextual ambient variables?
– pixelbits
Nov 22 at 2:05












@mjwlls, yup that's a duplicate - that's exactly what i'm trying to do
– pixelbits
Nov 22 at 2:17




@mjwlls, yup that's a duplicate - that's exactly what i'm trying to do
– pixelbits
Nov 22 at 2:17












Since its not available within the C# language, does anyone know if there is a library? I know its been recommended to use CSharpScript.EvaluateAsync in the other answer, but I need to be able to pass in complex objects in the ambient context, not just string variables.
– pixelbits
Nov 22 at 2:34






Since its not available within the C# language, does anyone know if there is a library? I know its been recommended to use CSharpScript.EvaluateAsync in the other answer, but I need to be able to pass in complex objects in the ambient context, not just string variables.
– pixelbits
Nov 22 at 2:34














Actually, never mind. CSharpScript.EvaluateAsync works for me, it supports complex global context parameters.
– pixelbits
Nov 22 at 2:43




Actually, never mind. CSharpScript.EvaluateAsync works for me, it supports complex global context parameters.
– pixelbits
Nov 22 at 2:43












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










You are missing a $



var name = "mike";
var desc = $"hello world {name}"; // this needs be interpolated as well
var t = $"{desc}";
Console.WriteLine(t); // PRINTS: hello world mike




Additional Resources



$ - string interpolation (C# Reference)




The $ special character identifies a string literal as an interpolated
string
. An interpolated string is a string literal that might contain
interpolated expressions. When an interpolated string is resolved to a
result string, items with interpolated expressions are replaced by the
string representations of the expression results. This feature is
available in C# 6 and later versions of the language.






Update




but suppose I want to have a variable storing the string with {name}
in it. Is there no way to achieve interpolation if its in a variable?




No you would have to use standard String.Format Tokens



var tokenString = "Something {0}";

String.Format(tokenString,someVariable);




String.Format Method




Converts the value of objects to strings based on the formats
specified and inserts them into another string.



Use String.Format if you need to insert the value of an object,
variable, or expression into another string. For example, you can
insert the value of a Decimal value into a string to display it to the
user as a single string:




Composite Formatting




The .NET composite formatting feature takes a list of objects and a
composite format string as input. A composite format string consists
of fixed text intermixed with indexed placeholders, called format
items, that correspond to the objects in the list. The formatting
operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.







share|improve this answer























  • I'm trying to keep desc as a variable. i.e. suppose I want to pass in the string as a variable. Is there a way to interpolate the variable?
    – pixelbits
    Nov 22 at 2:08










  • You have more details so I'll remove mine as it basically has the same. Looks like a dupe was found.
    – Nkosi
    Nov 22 at 2:17












  • @Nkosi all good we answered it at the same time, its a sloooow day. Hence the excessive details
    – TheGeneral
    Nov 22 at 2:18












  • Thanks for the feedback guys, really appreciate it. It's too bad there isn't a way to do this using interpolated strings. I can probably do it with string.format, but it aint gonna be pretty
    – pixelbits
    Nov 22 at 2:26




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










You are missing a $



var name = "mike";
var desc = $"hello world {name}"; // this needs be interpolated as well
var t = $"{desc}";
Console.WriteLine(t); // PRINTS: hello world mike




Additional Resources



$ - string interpolation (C# Reference)




The $ special character identifies a string literal as an interpolated
string
. An interpolated string is a string literal that might contain
interpolated expressions. When an interpolated string is resolved to a
result string, items with interpolated expressions are replaced by the
string representations of the expression results. This feature is
available in C# 6 and later versions of the language.






Update




but suppose I want to have a variable storing the string with {name}
in it. Is there no way to achieve interpolation if its in a variable?




No you would have to use standard String.Format Tokens



var tokenString = "Something {0}";

String.Format(tokenString,someVariable);




String.Format Method




Converts the value of objects to strings based on the formats
specified and inserts them into another string.



Use String.Format if you need to insert the value of an object,
variable, or expression into another string. For example, you can
insert the value of a Decimal value into a string to display it to the
user as a single string:




Composite Formatting




The .NET composite formatting feature takes a list of objects and a
composite format string as input. A composite format string consists
of fixed text intermixed with indexed placeholders, called format
items, that correspond to the objects in the list. The formatting
operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.







share|improve this answer























  • I'm trying to keep desc as a variable. i.e. suppose I want to pass in the string as a variable. Is there a way to interpolate the variable?
    – pixelbits
    Nov 22 at 2:08










  • You have more details so I'll remove mine as it basically has the same. Looks like a dupe was found.
    – Nkosi
    Nov 22 at 2:17












  • @Nkosi all good we answered it at the same time, its a sloooow day. Hence the excessive details
    – TheGeneral
    Nov 22 at 2:18












  • Thanks for the feedback guys, really appreciate it. It's too bad there isn't a way to do this using interpolated strings. I can probably do it with string.format, but it aint gonna be pretty
    – pixelbits
    Nov 22 at 2:26

















up vote
3
down vote



accepted










You are missing a $



var name = "mike";
var desc = $"hello world {name}"; // this needs be interpolated as well
var t = $"{desc}";
Console.WriteLine(t); // PRINTS: hello world mike




Additional Resources



$ - string interpolation (C# Reference)




The $ special character identifies a string literal as an interpolated
string
. An interpolated string is a string literal that might contain
interpolated expressions. When an interpolated string is resolved to a
result string, items with interpolated expressions are replaced by the
string representations of the expression results. This feature is
available in C# 6 and later versions of the language.






Update




but suppose I want to have a variable storing the string with {name}
in it. Is there no way to achieve interpolation if its in a variable?




No you would have to use standard String.Format Tokens



var tokenString = "Something {0}";

String.Format(tokenString,someVariable);




String.Format Method




Converts the value of objects to strings based on the formats
specified and inserts them into another string.



Use String.Format if you need to insert the value of an object,
variable, or expression into another string. For example, you can
insert the value of a Decimal value into a string to display it to the
user as a single string:




Composite Formatting




The .NET composite formatting feature takes a list of objects and a
composite format string as input. A composite format string consists
of fixed text intermixed with indexed placeholders, called format
items, that correspond to the objects in the list. The formatting
operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.







share|improve this answer























  • I'm trying to keep desc as a variable. i.e. suppose I want to pass in the string as a variable. Is there a way to interpolate the variable?
    – pixelbits
    Nov 22 at 2:08










  • You have more details so I'll remove mine as it basically has the same. Looks like a dupe was found.
    – Nkosi
    Nov 22 at 2:17












  • @Nkosi all good we answered it at the same time, its a sloooow day. Hence the excessive details
    – TheGeneral
    Nov 22 at 2:18












  • Thanks for the feedback guys, really appreciate it. It's too bad there isn't a way to do this using interpolated strings. I can probably do it with string.format, but it aint gonna be pretty
    – pixelbits
    Nov 22 at 2:26















up vote
3
down vote



accepted







up vote
3
down vote



accepted






You are missing a $



var name = "mike";
var desc = $"hello world {name}"; // this needs be interpolated as well
var t = $"{desc}";
Console.WriteLine(t); // PRINTS: hello world mike




Additional Resources



$ - string interpolation (C# Reference)




The $ special character identifies a string literal as an interpolated
string
. An interpolated string is a string literal that might contain
interpolated expressions. When an interpolated string is resolved to a
result string, items with interpolated expressions are replaced by the
string representations of the expression results. This feature is
available in C# 6 and later versions of the language.






Update




but suppose I want to have a variable storing the string with {name}
in it. Is there no way to achieve interpolation if its in a variable?




No you would have to use standard String.Format Tokens



var tokenString = "Something {0}";

String.Format(tokenString,someVariable);




String.Format Method




Converts the value of objects to strings based on the formats
specified and inserts them into another string.



Use String.Format if you need to insert the value of an object,
variable, or expression into another string. For example, you can
insert the value of a Decimal value into a string to display it to the
user as a single string:




Composite Formatting




The .NET composite formatting feature takes a list of objects and a
composite format string as input. A composite format string consists
of fixed text intermixed with indexed placeholders, called format
items, that correspond to the objects in the list. The formatting
operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.







share|improve this answer














You are missing a $



var name = "mike";
var desc = $"hello world {name}"; // this needs be interpolated as well
var t = $"{desc}";
Console.WriteLine(t); // PRINTS: hello world mike




Additional Resources



$ - string interpolation (C# Reference)




The $ special character identifies a string literal as an interpolated
string
. An interpolated string is a string literal that might contain
interpolated expressions. When an interpolated string is resolved to a
result string, items with interpolated expressions are replaced by the
string representations of the expression results. This feature is
available in C# 6 and later versions of the language.






Update




but suppose I want to have a variable storing the string with {name}
in it. Is there no way to achieve interpolation if its in a variable?




No you would have to use standard String.Format Tokens



var tokenString = "Something {0}";

String.Format(tokenString,someVariable);




String.Format Method




Converts the value of objects to strings based on the formats
specified and inserts them into another string.



Use String.Format if you need to insert the value of an object,
variable, or expression into another string. For example, you can
insert the value of a Decimal value into a string to display it to the
user as a single string:




Composite Formatting




The .NET composite formatting feature takes a list of objects and a
composite format string as input. A composite format string consists
of fixed text intermixed with indexed placeholders, called format
items, that correspond to the objects in the list. The formatting
operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 2:17

























answered Nov 22 at 2:05









TheGeneral

26.4k63163




26.4k63163












  • I'm trying to keep desc as a variable. i.e. suppose I want to pass in the string as a variable. Is there a way to interpolate the variable?
    – pixelbits
    Nov 22 at 2:08










  • You have more details so I'll remove mine as it basically has the same. Looks like a dupe was found.
    – Nkosi
    Nov 22 at 2:17












  • @Nkosi all good we answered it at the same time, its a sloooow day. Hence the excessive details
    – TheGeneral
    Nov 22 at 2:18












  • Thanks for the feedback guys, really appreciate it. It's too bad there isn't a way to do this using interpolated strings. I can probably do it with string.format, but it aint gonna be pretty
    – pixelbits
    Nov 22 at 2:26




















  • I'm trying to keep desc as a variable. i.e. suppose I want to pass in the string as a variable. Is there a way to interpolate the variable?
    – pixelbits
    Nov 22 at 2:08










  • You have more details so I'll remove mine as it basically has the same. Looks like a dupe was found.
    – Nkosi
    Nov 22 at 2:17












  • @Nkosi all good we answered it at the same time, its a sloooow day. Hence the excessive details
    – TheGeneral
    Nov 22 at 2:18












  • Thanks for the feedback guys, really appreciate it. It's too bad there isn't a way to do this using interpolated strings. I can probably do it with string.format, but it aint gonna be pretty
    – pixelbits
    Nov 22 at 2:26


















I'm trying to keep desc as a variable. i.e. suppose I want to pass in the string as a variable. Is there a way to interpolate the variable?
– pixelbits
Nov 22 at 2:08




I'm trying to keep desc as a variable. i.e. suppose I want to pass in the string as a variable. Is there a way to interpolate the variable?
– pixelbits
Nov 22 at 2:08












You have more details so I'll remove mine as it basically has the same. Looks like a dupe was found.
– Nkosi
Nov 22 at 2:17






You have more details so I'll remove mine as it basically has the same. Looks like a dupe was found.
– Nkosi
Nov 22 at 2:17














@Nkosi all good we answered it at the same time, its a sloooow day. Hence the excessive details
– TheGeneral
Nov 22 at 2:18






@Nkosi all good we answered it at the same time, its a sloooow day. Hence the excessive details
– TheGeneral
Nov 22 at 2:18














Thanks for the feedback guys, really appreciate it. It's too bad there isn't a way to do this using interpolated strings. I can probably do it with string.format, but it aint gonna be pretty
– pixelbits
Nov 22 at 2:26






Thanks for the feedback guys, really appreciate it. It's too bad there isn't a way to do this using interpolated strings. I can probably do it with string.format, but it aint gonna be pretty
– pixelbits
Nov 22 at 2:26





Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista