string-interpolation

How to use escape characters with string interpolation in C# 6?

 ̄綄美尐妖づ 提交于 2019-11-26 23:06:51
问题 I've been using string interpolation and loving it, however I have an issue where I am trying to include a backslash in my output, but am not able to get it to work. What I want is something like this.. var domain = "mydomain"; var userName = "myUserName"; var combo = $"{domain}\{userName}" I want the output of combo to be myDomain\myUserName What I am getting is a syntax error about the \ being an escape character. If I put in \\ then the snytax error is gone, but the output is myDomain\

How do you use verbatim strings with interpolation?

梦想的初衷 提交于 2019-11-26 22:46:49
问题 In C#6 there is a new feature: interpolated strings. These let you put expressions directly into code, rather than relying on indexes: string s = string.Format("Adding \"{0}\" and {1} to foobar.", x, this.Y()); Becomes: string s = $"Adding \"{x}\" and {this.Y()} to foobar."; However, we have a lot of strings across multiple lines using verbatim strings (mainly SQL statements) like this: string s = string.Format(@"Result... Adding ""{0}"" and {1} to foobar: {2}", x, this.Y(), x.GetLog());

String concatenation vs. interpolation in Ruby

倾然丶 夕夏残阳落幕 提交于 2019-11-26 22:07:14
I am just starting to learn Ruby (first time programming), and have a basic syntactical question with regards to variables, and various ways of writing code. Chris Pine's "Learn to Program" taught me to write a basic program like this... num_cars_again= 2 puts 'I own ' + num_cars_again.to_s + ' cars.' This is fine, but then I stumbled across the tutorial on ruby.learncodethehardway.com, and was taught to write the same exact program like this... num_cars= 2 puts "I own #{num_cars} cars." They both output the same thing, but obviously option 2 is a much shorter way to do it. Is there any

Python 3 returns “invalid syntax” when trying to perform string interpolation

白昼怎懂夜的黑 提交于 2019-11-26 20:49:27
I have recently been learning python 3 and I cannot get any examples involving string interpolation (formatting) to work. In [1]: state = "Washington" In [2]: state Out[2]: 'Washington' In [3]: my_message = f"I live in {state}" File "<ipython-input-3-d004dd9e0255>", line 1 my_message = f"I live in {state}" ^ SyntaxError: invalid syntax I figured my machine was defaulting to python 2, but a quick check reveals: Python 3.5.2 (default, Nov 17 2016, 17:05:23) Type "copyright", "credits" or "license" for more information. IPython 5.2.2 -- An enhanced Interactive Python. I am on Ubuntu 16.04:

Using C# 6 features with CodeDomProvider (Roslyn)

﹥>﹥吖頭↗ 提交于 2019-11-26 18:15:01
问题 CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp" ); CompilerParameters objCompilerParameters = new CompilerParameters(); ... CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile( objCompilerParameters, files.ToArray() ); When I compile my files I get: FileFunctions.cs(347): Error: Unexpected character '$' Does anyone know how to get string interpolation working with CodeDom compiling? I found this link: How to target .net 4.5 with

What is the final format for string interpolation in VS 2015?

廉价感情. 提交于 2019-11-26 16:48:44
问题 I can't get string interpolation to work. Last news from MS I found was http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx However all that is said there is not working. Anyone knows if string interpolation made it into VS 2015? Is there any documentation about it? Can one you give an example? For instance, none of these formats work ( edited ): int i = 42; var s = "\{i}"; // correction after jon's answer: this works! var s = $"{i}"; // compiler error var s = "{{i}}

How do I interpolate strings?

梦想与她 提交于 2019-11-26 16:24:26
I want to do the following in C# (coming from a Python background): strVar = "stack" mystr = "This is %soverflow" % (strVar) How do I replace the token inside the string with the value outside of it? Darin Dimitrov string mystr = string.Format("This is {0}overflow", strVar); And you could also use named parameters instead of indexes. Ashtonian This has been added as of C# 6.0 (Visual Studio 2015+). Example: var planetName = "Bob"; var myName = "Ford"; var formattedStr = $"Hello planet {planetName}, my name is {myName}!"; // formattedStr should be "Hello planet Bob, my name is Ford!" This is

Why does string interpolation work in Ruby when there are no curly braces?

不想你离开。 提交于 2019-11-26 16:13:01
The proper way to use string interpolation in Ruby is as follows: name = "Ned Stark" puts "Hello there, #{name}" #=> "Hello there, Ned Stark" That is the way I intend to always use it. However, I've noticed something odd in Ruby's string interpolation. I've noticed that string interpolation works in Ruby without the curly braces in regards to instance variables. For example: @name = "Ned Stark" puts "Hello there, #@name" #=> "Hello there, Ned Stark" And that trying the same thing as a non-instance variable does not work. name = "Ned Stark" puts "Hello, there, #name" #=> "Hello there, #name" I

String interpolation in Scala 2.10 - How to interpolate a String variable?

隐身守侯 提交于 2019-11-26 16:04:37
问题 String interpolation is available in Scala starting Scala 2.10 This is the basic example val name = "World" //> name : String = World val message = s"Hello $name" //> message : String = Hello World I was wondering if there is a way to do dynamic interpolation, e.g. the following (doesn't compile, just for illustration purposes) val name = "World" //> name : String = World val template = "Hello $name" //> template : String = Hello $name //just for illustration: val message = s(template) //>

How to use the ternary operator inside an interpolated string?

喜欢而已 提交于 2019-11-26 15:52:55
I'm confused as to why this code won't compile: var result = $"{fieldName}{isDescending ? " desc" : string.Empty}"; If I split it up, it works fine: var desc = isDescending ? " desc" : string.Empty; var result = $"{fieldName}{desc}"; According to the documentation : The structure of an interpolated string is as follows: { <interpolationExpression>[,<alignment>][:<formatString>] } The problem is that the colon is used to denote formatting, like: Console.WriteLine($"The current hour is {hours:hh}") The solution is to wrap the conditional in parenthesis: var result = $"Descending {(isDescending ?